#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0;
  bool t = false;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') t = true, ch = getchar();
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
  return t ? -x : x;
}
int fpow(int a, int b) {
  int s = 1;
  while (b) {
    if (b & 1) s = 1ll * s * a % 1000000007;
    a = 1ll * a * a % 1000000007;
    b >>= 1;
  }
  return s;
}
int n, a[200200], tot, ans;
int cnt[200200];
int c1[200200], c2[200200];
int lb(int x) { return x & (-x); }
void add(int x, int w) {
  while (x <= n) c1[x] = (c1[x] + w) % 1000000007, ++c2[x], x += lb(x);
}
int getsum1(int x) {
  int ret = 0;
  while (x) ret = (ret + c1[x]) % 1000000007, x -= lb(x);
  return ret;
}
int getsum2(int x) {
  int ret = 0;
  while (x) ret += c2[x], x -= lb(x);
  return ret;
}
int D[200200], invD[200200], zero[200200], S[200200];
int main() {
  n = read();
  tot = 1;
  for (int i = 1; i <= n; ++i) ++cnt[a[i] = read()];
  for (int i = n - 1; i; --i) cnt[i] += cnt[i + 1];
  for (int i = 1; i <= n; ++i)
    tot = 1ll * tot * (cnt[i] -= (n - i)) % 1000000007;
  if (!tot) {
    puts("0");
    return 0;
  }
  S[0] = D[0] = 1;
  for (int i = 1; i <= n; ++i) {
    int x = 1ll * (cnt[i] - 1) * fpow(cnt[i], 1000000007 - 2) % 1000000007;
    if (!x)
      S[zero[i] = zero[i - 1] + 1] = i, D[i] = D[i - 1];
    else
      zero[i] = zero[i - 1], D[i] = 1ll * D[i - 1] * x % 1000000007;
    invD[i] = fpow(D[i], 1000000007 - 2);
  }
  for (int i = 1; i <= n; ++i) {
    ans =
        (ans + 1ll * (getsum1(a[i]) - getsum1(S[zero[a[i]]] - 1) + 1000000007) *
                   D[a[i]] % 1000000007 * tot % 1000000007 * 500000004 %
                   1000000007) %
        1000000007;
    add(a[i], invD[a[i]]);
  }
  for (int i = 1; i <= n; ++i) c1[i] = c2[i] = 0;
  for (int i = n; i; --i) {
    ans -= 1ll * (getsum1(a[i] - 1) - getsum1(S[zero[a[i]]] - 1) + 1000000007) *
           D[a[i]] % 1000000007 * tot % 1000000007 * 500000004 % 1000000007;
    ans = (ans + 1ll * getsum2(a[i] - 1) * tot) % 1000000007;
    ans = (ans + 1000000007) % 1000000007;
    add(a[i], invD[a[i]]);
  }
  printf("%d\n", ans);
  return 0;
}
