#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch = getchar();
  int h = 0, t = 1;
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch >= '0' && ch <= '9') h = h * 10 + ch - '0', ch = getchar();
  return h * t;
}
const int N = 2e5 + 10, mod = 1e9 + 7, inv = 5e8 + 4;
int n, ans, tot = 1, D[N], C[N], t1[N], t2[N], lst[N], A[N], cnt[N];
void add(int &x, int y) {
  x += y;
  if (x > mod) x -= mod;
}
void Add(int x, int k) {
  while (x <= n) add(t1[x], k), t2[x]++, x += x & (-x);
}
int Sum1(int x) {
  int s = 0;
  while (x) add(s, t1[x]), x -= x & (-x);
  return s;
}
int Sum2(int x) {
  int s = 0;
  while (x) s += t2[x], x -= x & (-x);
  return s;
}
int ksm(int x, int k) {
  int s = 1;
  for (; k; k >>= 1, x = 1ll * x * x % mod)
    if (k & 1) s = 1ll * s * x % mod;
  return s;
}
int main() {
  n = read();
  D[0] = 1;
  for (int i = 1; i <= n; i++) A[i] = read(), cnt[A[i]]++;
  for (int i = n; i >= 1; i--) cnt[i] += cnt[i + 1];
  for (int i = 1; i <= n; i++)
    cnt[i] -= (n - i), tot = 1ll * tot * cnt[i] % mod;
  if (tot <= 0) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    int x = 1ll * (cnt[i] - 1) * ksm(cnt[i], mod - 2) % mod;
    if (!x)
      lst[i] = i, D[i] = D[i - 1];
    else
      lst[i] = lst[i - 1], D[i] = 1ll * D[i - 1] * x % mod;
    C[i] = ksm(D[i], mod - 2);
  }
  for (int i = 1, s = A[1]; i <= n; i++) {
    add(ans, 1ll * (Sum1(s) - Sum1(lst[s])) % mod * tot % mod * inv % mod *
                 D[s] % mod);
    Add(s, C[s]);
    s = A[i + 1];
    ans = (ans + mod) % mod;
  }
  for (int i = 1; i <= n; i++) t1[i] = t2[i] = 0;
  for (int i = n, s = A[n]; i; i--) {
    add(ans, 1ll * Sum2(s - 1) * tot % mod);
    add(ans, -1ll * (Sum1(s - 1) - Sum1(lst[s])) % mod * tot % mod * inv % mod *
                 D[s] % mod);
    Add(s, C[s]);
    s = A[i - 1];
    (ans += mod) %= mod;
  }
  printf("%d\n", (ans + mod) % mod);
}
