#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &s) {
  s = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) s = ((s + (s << 2)) << 1) + ch - '0', ch = getchar();
}
int n, A[210000], C[210000], S = 1, tl, Ans, bel[210000];
vector<int> v[210000];
pair<int, int> T[210000];
inline int Mod(int u) { return u < 1000000007 ? u : u - 1000000007; }
struct Bit {
  int tmp[210000];
  Bit() { memset(tmp, 0, sizeof(tmp)); }
  inline void Insert(int x, int val) {
    for (; x <= n; x += (x) & (-x)) tmp[x] = Mod(val + tmp[x]);
  }
  inline int Query(int x) {
    int Sum = 0;
    for (; x; x -= (x) & (-x)) Sum = Mod(tmp[x] + Sum);
    return Sum;
  }
} Sum1, cnt, Sum2;
inline int Fast_Power(int u, int k = 1000000007 - 2) {
  int Sum = 1;
  while (k) {
    if (k & 1) Sum = 1ll * Sum * u % 1000000007;
    u = 1ll * u * u % 1000000007, k >>= 1;
  }
  return Sum;
}
int main() {
  read(n), T[0] = make_pair(1, 0);
  for (int i = 1; i <= n; ++i) read(A[i]), C[A[i]]++;
  for (int i = n; i; --i) C[i] += C[i + 1];
  for (int i = 1; i <= n; ++i)
    C[i] = C[i] - n + i, S = 1ll * S * C[i] % 1000000007;
  if (S <= 0) return puts("0"), 0;
  for (int i = 1; i <= n; ++i) {
    T[i] = T[i - 1];
    if (C[i] == 1)
      T[i].second++, ++tl, bel[i] = tl;
    else
      T[i].first = 1ll * T[i].first * (C[i] - 1) % 1000000007 *
                   Fast_Power(C[i]) % 1000000007,
      bel[i] = tl;
  }
  for (int i = 1; i <= n; ++i)
    Ans =
        Mod(Ans +
            1ll * Mod(Mod(i - cnt.Query(A[i]) + 1000000007) - 1 + 1000000007) *
                S % 1000000007),
    cnt.Insert(A[i], 1), v[bel[A[i]]].push_back(i);
  for (int i = 0; i <= tl; ++i) {
    for (auto x : v[i]) {
      Ans = Mod(Ans + 1ll * (Sum2.Query(A[x])) * T[A[x]].first % 1000000007 *
                          S % 1000000007 * 500000004 % 1000000007);
      Ans = Mod(Ans -
                1ll * Mod(Sum1.Query(n) - Sum1.Query(A[x]) + 1000000007) *
                    Fast_Power(T[A[x]].first) % 1000000007 * S % 1000000007 *
                    500000004 % 1000000007 +
                1000000007);
      Sum1.Insert(A[x], T[A[x]].first),
          Sum2.Insert(A[x], Fast_Power(T[A[x]].first));
    }
    for (auto x : v[i])
      Sum1.Insert(A[x], -T[A[x]].first),
          Sum2.Insert(A[x], -Fast_Power(T[A[x]].first));
  }
  printf("%d\n", Ans);
  return 0;
}
