#include <bits/stdc++.h>
long long gi() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
int pow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1) ret = 1ll * ret * x % 1000000007;
    x = 1ll * x * x % 1000000007;
    y >>= 1;
  }
  return ret;
}
int n, A[200010], c[200010], C[200010], D[200010], iD[200010], d[200010],
    id[200010];
int t[200010], T[200010], TT[200010], from[200010];
void add(int& x, int y) {
  x = x + y >= 1000000007 ? x + y - 1000000007 : x + y;
}
void update(int* t, int x, int y) {
  while (x <= n) add(t[x], y), x += x & -x;
}
int query(int* t, int x) {
  int r = 0;
  while (x) add(r, t[x]), x -= x & -x;
  return r;
}
int main() {
  n = gi();
  for (int i = 1; i <= n; ++i) ++c[A[i] = gi()];
  for (int i = n; i; --i) C[i] = c[i] + C[i + 1];
  int S = 1;
  for (int i = 1; i <= n; ++i)
    S = 1ll * S * std::max(0, C[i] - n + i) % 1000000007;
  if (!S) return puts("0"), 0;
  for (int i = 1; i <= n; ++i)
    d[i] = 1ll * (C[i] - n + i - 1) * pow(C[i] - n + i, 1000000007 - 2) %
           1000000007,
    id[i] = pow(d[i], 1000000007 - 2);
  for (int i = 1; i <= n; ++i) {
    from[i] = d[i] ? from[i - 1] : i;
    if (d[i - 1])
      d[i] = 1ll * d[i] * d[i - 1] % 1000000007,
      id[i] = 1ll * id[i] * id[i - 1] % 1000000007;
  }
  for (int i = 1; i <= n; ++i) D[i] = d[A[i]], iD[i] = id[A[i]];
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    ans = (ans + 1ll * c[i] * (c[i] - 1) / 2) % 1000000007;
  ans = 1ll * ans * S % 1000000007 * (1000000007 + 1 >> 1) % 1000000007;
  for (int l = 1, r; l <= n; l = r + 1) {
    r = l;
    while (from[r + 1] == from[l]) ++r;
    for (int i = l; i <= r; ++i) {
      add(ans, 1ll * D[i] * query(t, A[i] - 1) % 1000000007 * S % 1000000007 *
                   (1000000007 + 1 >> 1) % 1000000007);
      update(t, A[i], iD[i]);
    }
    for (int i = l; i <= r; ++i) update(t, A[i], 1000000007 - iD[i]);
    for (int i = r; i >= l; --i) {
      add(ans, (1ll * S * query(T, A[i] - 1) % 1000000007 -
                1ll * S * D[i] % 1000000007 * query(t, A[i] - 1) % 1000000007 *
                    (1000000007 + 1 >> 1) % 1000000007 +
                1000000007) %
                   1000000007);
      update(t, A[i], iD[i]);
      update(T, A[i], 1);
    }
    for (int i = l; i <= r; ++i)
      update(t, A[i], 1000000007 - iD[i]), update(T, A[i], 1000000007 - 1);
    for (int i = l; i <= r; ++i)
      add(ans, 1ll * S * (l - 1 - query(TT, A[i])) % 1000000007);
    for (int i = l; i <= r; ++i) update(TT, A[i], 1);
  }
  printf("%d\n", ans);
  return 0;
}
