#include <bits/stdc++.h>
using namespace std;
struct segtree {
  long long int seg[256 * 1024 * 2], mul[200005];
  int mum;
  void init(int n) {
    mum = 1;
    while (mum < n) mum <<= 1;
    for (int i = 0; i < mum * 2; i++) seg[i] = 0, mul[i] = 1;
  }
  void pass(int k) {
    for (int t = k * 2 + 1; t <= k * 2 + 2; t++) {
      mul[t] = mul[t] * mul[k] % 1000000007;
      seg[t] = seg[t] * mul[k] % 1000000007;
    }
    mul[k] = 1;
  }
  void update(int a, int b, int k, int l, int r, long long int x) {
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      seg[k] *= x;
      mul[k] *= x;
      seg[k] %= 1000000007;
      mul[k] %= 1000000007;
    } else {
      pass(k);
      update(a, b, k * 2 + 1, l, (l + r) / 2, x);
      update(a, b, k * 2 + 2, (l + r) / 2, r, x);
      seg[k] = (seg[k * 2 + 1] + seg[k * 2 + 2]) % 1000000007;
    }
  }
  void update(int a, int b, long long int x) { update(a, b, 0, 0, mum, x); }
  long long int get(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a) return 0;
    if (a <= l && r <= b)
      return seg[k];
    else {
      pass(k);
      long long int vl = get(a, b, k * 2 + 1, l, (l + r) / 2);
      long long int vr = get(a, b, k * 2 + 2, (l + r) / 2, r);
      return (vl + vr) % 1000000007;
    }
  }
  long long int get(int a, int b) { return get(a, b, 0, 0, mum); }
  void add(int p, long long int x) {
    int l = 0, r = mum, k = 0;
    while (r - l > 1) {
      pass(k);
      int m = (l + r) / 2;
      if (p < m) {
        r = m;
        k = k * 2 + 1;
      } else {
        l = m;
        k = k * 2 + 2;
      }
    }
    seg[k] = (seg[k] + x) % 1000000007;
    while (k > 0) {
      k = (k - 1) / 2;
      seg[k] = (seg[k * 2 + 1] + seg[k * 2 + 2]) % 1000000007;
    }
  }
};
segtree seg;
long long int inv[200005], fac[200005], finv[200005];
void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < 200005; i++) {
    inv[i] = 1000000007 - inv[1000000007 % i] * (1000000007 / i) % 1000000007;
    fac[i] = fac[i - 1] * (long long int)i % 1000000007;
    finv[i] = finv[i - 1] * inv[i] % 1000000007;
  }
}
long long int C(int a, int b) {
  if (a < b) return 0;
  return fac[a] * (finv[b] * finv[a - b] % 1000000007) % 1000000007;
}
int A[200005];
vector<int> vec[200005];
int cnt[200005];
int sum[200005];
long long int all;
int n;
long long int solve() {
  for (int i = 1; i <= n; i++) vec[i].clear();
  for (int i = 0; i < n; i++) vec[A[i]].push_back(i);
  seg.init(n + 2);
  for (int i = n - 1; i >= 0; i--) {
    sum[i] = (int)seg.get(0, A[i]);
    seg.add(A[i], 1);
  }
  seg.init(n + 2);
  long long int now = 0;
  long long int ret = 0;
  for (int i = n; i >= 1; i--) {
    for (int j = 0; j < vec[i].size(); j++) {
      int v = vec[i][j];
      now -= seg.get(0, v);
      if (now < 0) now += 1000000007;
      now += sum[v];
    }
    for (int j = 0; j < vec[i].size(); j++) {
      int v = vec[i][j];
      seg.add(v, 1);
    }
    ret += all * now % 1000000007 * inv[cnt[i]] % 1000000007;
    if (ret >= 1000000007) ret -= 1000000007;
    now = now * (long long int)(cnt[i] - 1) % 1000000007;
    now = now * inv[cnt[i]] % 1000000007;
    seg.update(0, seg.mum,
               (long long int)(cnt[i] - 1) * inv[cnt[i]] % 1000000007);
  }
  return ret;
}
int main() {
  make();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    A[i] = a;
  }
  for (int i = 0; i < n; i++) cnt[A[i]]++;
  for (int i = n - 1; i >= 1; i--) cnt[i] += cnt[i + 1];
  all = 1;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] <= n - i) {
      puts("0");
      return 0;
    }
    cnt[i] -= n - i;
    all = all * (long long int)cnt[i] % 1000000007;
  }
  long long int ret = (long long int)n * (long long int)(n - 1) / 2LL;
  ret %= 1000000007;
  ret *= all;
  ret %= 1000000007;
  ret += solve();
  if (ret >= 1000000007) ret -= 1000000007;
  for (int l = 0, r = n - 1; l < r; l++, r--) swap(A[l], A[r]);
  ret -= solve();
  if (ret < 0) ret += 1000000007;
  if (ret % 2 == 1) ret += 1000000007;
  printf("%lld\n", ret / 2);
  return 0;
}
