#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch;
  for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
    ;
  int x = ch - '0';
  for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x;
}
const int N = 2e5 + 5, Mo = 1e9 + 7;
void mod(int &x) { x = x >= Mo ? x - Mo : x; }
void inc(int &x, int y) { x = x + y >= Mo ? x + y - Mo : x + y; }
int pwr(int x, int y) {
  int z = 1;
  for (; y; y >>= 1, x = (long long)x * x % Mo)
    if (y & 1) z = (long long)z * x % Mo;
  return z;
}
int n, ql, qr, tmp, a[N], cnt[N], t[N], tag[N << 2], tr[N << 2];
vector<int> p[N];
void Ins(int x) {
  for (; x <= n; x += x & -x) t[x]++;
}
int Que(int x) {
  int res = 0;
  for (; x; x -= x & -x) res += t[x];
  return res;
}
void mult(int v, int z) {
  tr[v] = (long long)tr[v] * z % Mo;
  tag[v] = (long long)tag[v] * z % Mo;
}
void down(int v) {
  if (tag[v] > 1) {
    mult(v << 1, tag[v]);
    mult(v << 1 | 1, tag[v]);
    tag[v] = 1;
  }
}
void ins(int v, int l, int r, int x) {
  if (l == r) {
    tr[v] = tmp;
    return;
  }
  int mid = l + r >> 1;
  down(v);
  if (x <= mid)
    ins(v << 1, l, mid, x);
  else
    ins(v << 1 | 1, mid + 1, r, x);
  mod(tr[v] = tr[v << 1] + tr[v << 1 | 1]);
}
int query(int v, int l, int r) {
  if (ql <= l && r <= qr) return tr[v];
  int mid = l + r >> 1;
  down(v);
  int sum = 0;
  if (ql <= mid) inc(sum, query(v << 1, l, mid));
  if (qr > mid) inc(sum, query(v << 1 | 1, mid + 1, r));
  return sum;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    cnt[a[i]]++;
    p[a[i]].push_back(i);
  }
  for (int i = n; i >= 1; i--) cnt[i] += cnt[i + 1];
  int sum = 1, inv = pwr(2, Mo - 2);
  for (int i = 1; i <= n; i++) {
    if (cnt[i] - n + i < 0) {
      puts("0");
      return 0;
    }
    sum = (long long)sum * (cnt[i] - n + i) % Mo;
  }
  tmp = (long long)sum * inv % Mo;
  for (int i = 1; i <= n << 2; i++) tag[i] = 1;
  int ans = 0;
  for (int i = n; i >= 1; i--) {
    if (!p[i].empty()) {
      int sz = p[i].size();
      for (int j = 0; j <= sz - 1; j++) {
        int x = p[i][j];
        ql = x;
        qr = n;
        inc(ans, query(1, 1, n));
        inc(ans, (long long)sum * Que(x) % Mo);
        ql = 1;
        qr = x;
        inc(ans, Mo - query(1, 1, n));
      }
      inc(ans, (long long)sz * (sz - 1) % Mo * inv % Mo * sum % Mo * inv % Mo);
      for (int j = 0; j <= sz - 1; j++) ins(1, 1, n, p[i][j]), Ins(p[i][j]);
    }
    mult(1, (long long)(cnt[i] - n + i - 1) * pwr(cnt[i] - n + i, Mo - 2) % Mo);
  }
  printf("%d\n", ans);
  return 0;
}
