#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005, MOD = 1000000007;
int n, a[MAXN], b[MAXN], cnt[MAXN], inv[MAXN], C[MAXN], pre[MAXN][2];
void add(int x, int d) {
  for (; x <= n; x += x & -x) (C[x] += d) %= MOD;
}
int sum(int x) {
  int ret = 0;
  for (; x; x -= x & -x) (ret += C[x]) %= MOD;
  return ret;
}
int work(int s) {
  for (int i = 1; i <= n; i++) C[i] = 0;
  int ret = 0;
  for (int i = 1; i <= n; i++) {
    add(a[i] + 1, pre[a[i]][1]);
    int k = (sum(a[i]) - sum(b[a[i]]) + MOD) % MOD;
    ret = (ret + (long long)k * pre[a[i]][0]) % MOD;
  }
  return (long long)ret * s % MOD * inv[2] % MOD;
}
int main() {
  scanf("%d", &n);
  inv[1] = 1;
  for (int i = 2; i <= n; i++)
    inv[i] = (long long)(MOD - MOD / i) * inv[MOD % i] % MOD;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    cnt[a[i]]++;
  }
  int all = 1;
  for (int i = 1, sum = 0, pro; i <= n; i++) {
    if (i <= sum) {
      printf("0");
      return 0;
    }
    if (i - sum == 1) {
      pre[i][0] = pre[i][1] = 1;
      b[i] = i;
    } else {
      pre[i][0] =
          (long long)pre[i - 1][0] * inv[i - sum] % MOD * (i - sum - 1) % MOD;
      pre[i][1] =
          (long long)pre[i - 1][1] * (i - sum) % MOD * inv[i - sum - 1] % MOD;
      b[i] = b[i - 1];
    }
    all = (long long)all * (i - sum) % MOD;
    (sum += cnt[i]) %= MOD;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = (ans + (long long)cnt[i] * (cnt[i] - 1) / 2) % MOD;
  ans = (long long)ans * inv[2] % MOD;
  for (int i = n; i >= 1; i--) {
    add(a[i], 1);
    (ans += sum(a[i] - 1)) % MOD;
  }
  ans = (long long)ans * all % MOD;
  (ans += work(all)) %= MOD;
  reverse(a + 1, a + n + 1);
  (ans += MOD - work(all)) %= MOD;
  printf("%d", ans);
  return 0;
}
