#include <bits/stdc++.h>
using i64 = long long;
const i64 MOD = 1000000007;
const int N = 200000 + 7;
int n;
int a[N], c[N], e[N];
int cril, crir;
i64 f[N], g[N], h[N];
i64 ans, s = 1;
i64 pow(i64 a, i64 k) {
  i64 t = 1;
  for (; k; a = a * a % MOD, k >>= 1)
    if (k & 1) t = t * a % MOD;
  return t;
}
i64 bit[N];
void add(int i, i64 v) {
  for (; i <= n; i += i & -i) (bit[i] += v) %= MOD;
}
i64 sum(int i) {
  i64 t = 0;
  for (; i; i -= i & -i) t += bit[i];
  return t % MOD;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), ++c[a[i]];
  for (int i = n; i; --i) {
    c[i] += c[i + 1];
    if (c[i] - n + i - 1 == 0) cril = i, crir = crir ? crir : i;
    f[i] = (c[i] - n + i - 1) * pow(c[i] - n + i, MOD - 2) % MOD;
    s = s * std::max(c[i] - n + i, 0) % MOD;
  }
  for (int i = g[0] = h[0] = 1; i <= n; ++i)
    g[i] = g[i - 1] * (f[i] ? f[i] : 1) % MOD,
    h[i] = h[i - 1] * pow(f[i] ? f[i] : 1, MOD - 2) % MOD;
  for (int i = 1; i <= n; ++i) e[c[a[i]]--] = i;
  for (int i = 1; i <= n; ++i) {
    if (a[e[i]] <= crir) continue;
    (ans += (sum(n) - sum(e[i]) - sum(e[i] - 1)) * h[a[e[i]]]) %= MOD;
    add(e[i], g[a[e[i]]]);
  }
  std::fill_n(bit + 1, n, 0);
  for (int i = 1; i <= n; ++i) {
    if (a[e[i]] >= cril) continue;
    (ans += (sum(n) - sum(e[i]) - sum(e[i] - 1)) * h[a[e[i]]]) %= MOD;
    add(e[i], g[a[e[i]]]);
  }
  std::fill_n(bit + 1, n, 0);
  for (int i = 1; i <= n; ++i) ans += sum(e[i] - 1), add(e[i], 2);
  ans = (ans % MOD * s % MOD * (-MOD / 2) % MOD + MOD) % MOD;
  printf("%lld\n", ans);
  return 0;
}
