#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, mo = 1e9 + 7;
struct rec {
  int sum, cnt;
} tr[N * 4], tmp;
int tag[N * 4];
rec operator+(rec a, rec b) { return {(a.sum + b.sum) % mo, a.cnt + b.cnt}; }
int n, a[N], cnt[N], prod, i, x, fac, d[N], ans, rev2;
int tt, b[N], nxt[N], fst[N];
void cr(int x, int y) {
  tt++;
  b[tt] = y;
  nxt[tt] = fst[x];
  fst[x] = tt;
}
int ksm(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1) ret = 1ll * ret * x % mo;
    y >>= 1;
    x = 1ll * x * x % mo;
  }
  return ret;
}
void dw(int x, int s) {
  if (tag[x] == 1) return;
  if (!s)
    tag[x * 2] = 1ll * tag[x * 2] * tag[x] % mo,
            tag[x * 2 + 1] = 1ll * tag[x * 2 + 1] * tag[x] % mo;
  tr[x].sum = 1ll * tr[x].sum * tag[x] % mo;
  tag[x] = 1;
}
rec get(int x, int l, int r, int i, int j) {
  if (l == i && r == j) return tr[x];
  int m = l + r >> 1;
  dw(x * 2, l == m);
  dw(x * 2 + 1, m + 1 == r);
  rec ret = {0, 0};
  if (i <= m) ret = ret + get(x * 2, l, m, i, min(m, j));
  if (m < j) ret = ret + get(x * 2 + 1, m + 1, r, max(i, m + 1), j);
  return ret;
}
void change(int x, int l, int r, int p, int v) {
  if (l == r) {
    tr[x].cnt = 1, tr[x].sum = v;
    return;
  }
  int m = l + r >> 1;
  dw(x * 2, l == m);
  dw(x * 2 + 1, m + 1 == r);
  if (p <= m)
    change(x * 2, l, m, p, v);
  else
    change(x * 2 + 1, m + 1, r, p, v);
  tr[x] = tr[x * 2] + tr[x * 2 + 1];
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", a + i);
    cr(a[i], i);
    cnt[a[i]]++;
  }
  for (i = n; i >= 1; i--) cnt[i] += cnt[i + 1];
  prod = 1;
  for (i = 1; i <= n; i++) prod = 1ll * prod * (cnt[i] - (n - i)) % mo;
  for (i = 1; i <= n * 4; i++) tag[i] = 1;
  rev2 = ksm(2, mo - 2);
  for (x = n; x >= 1; x--) {
    d[0] = 0;
    for (int p = fst[x]; p; p = nxt[p]) d[++d[0]] = b[p];
    fac = long long(cnt[x] - 1 - (n - x)) * ksm(cnt[x] - (n - x), mo - 2) % mo;
    for (i = 1; i <= d[0]; i++) {
      tmp = get(1, 1, n, d[i], n);
      ans = (ans + 1ll * tmp.sum * rev2) % mo;
      tmp = get(1, 1, n, 1, d[i]);
      ans = (ans + 1ll * tmp.cnt * prod - 1ll * tmp.sum * rev2) % mo;
    }
    ans = ans + 1ll * d[0] * (d[0] - 1) / 2 % mo * prod % mo * rev2 % mo;
    for (i = 1; i <= d[0]; i++) change(1, 1, n, d[i], prod);
    tag[1] = 1ll * tag[1] * fac % mo;
    dw(1, 1 == n);
  }
  if (ans < 0) ans += mo;
  printf("%d", ans);
}
