#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n;
int cnt[N], a[N], zero[N * 3];
long long t[N * 3], last[N], lasd[N], nex[N], rig[N];
int op[N * 3];
long long tot = 1;
const long long mod = 1e9 + 7;
struct node {
  int x, d;
  bool operator<(const node q) const { return d > q.d; }
} s[N];
long long ksm(long long x, long long t) {
  long long tot = 1;
  while (t) {
    if (t & 1) (tot *= x) %= mod;
    (x *= x) %= mod;
    t /= 2;
  }
  return tot;
}
void pushdown(int now) {
  if (zero[now]) {
    zero[now] = 0;
    zero[now << 1] = zero[now << 1 | 1] = 1;
    t[now << 1] = t[now << 1 | 1] = 0;
  }
}
void add(int now, int x, long long d, int l, int r) {
  t[now] += d;
  t[now] >= mod ? t[now] -= mod : 0;
  op[now]++;
  if (l == r) return;
  int mid = (l + r) / 2;
  pushdown(now);
  if (x <= mid)
    add(now << 1, x, d, l, mid);
  else
    add(now << 1 | 1, x, d, mid + 1, r);
}
long long get_sum(int now, int x, int y, int type, long long tmp, int l,
                  int r) {
  if (y < x) return 0;
  if (x == l && y == r) {
    if (type == 0)
      return (tot * op[now] % mod - t[now] * tmp % mod * 500000004ll % mod +
              mod) %
             mod;
    else
      return t[now] * tmp % mod * 500000004ll % mod;
  }
  int mid = (l + r) / 2;
  pushdown(now);
  if (y <= mid)
    return get_sum(now << 1, x, y, type, tmp, l, mid);
  else if (mid < x)
    return get_sum(now << 1 | 1, x, y, type, tmp, mid + 1, r);
  else
    return get_sum(now << 1, x, mid, type, tmp, l, mid) +
           get_sum(now << 1 | 1, mid + 1, y, type, tmp, mid + 1, r);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), cnt[a[i]]++, s[i] = (node){i, a[i]};
  for (int i = n; i >= 1; i--) cnt[i] += cnt[i + 1];
  last[0] = lasd[0] = 1;
  for (int i = 1; i <= n; i++) {
    cnt[i] -= n - i;
    if (cnt[i] <= 0) {
      printf("0");
      return 0;
    }
    last[i] = last[i - 1] * cnt[i];
    if (cnt[i] > 1)
      lasd[i] = lasd[i - 1] * (cnt[i] - 1) % mod;
    else
      lasd[i] = lasd[i - 1];
  }
  rig[n + 1] = n + 1, nex[n + 1] = 1;
  for (int i = n; i >= 1; i--)
    rig[i] = (cnt[i] != 1 ? rig[i + 1] : i), nex[i] = nex[i + 1] * cnt[i] % mod;
  tot = nex[1];
  sort(s + 1, s + 1 + n);
  long long ans = 0, tm;
  int now = 1, tmp;
  while (now <= n) {
    if (rig[s[now].d] != rig[s[now - 1].d]) zero[1] = true, t[1] = 0;
    tmp = now, tm = last[s[now].d] * ksm(lasd[s[now].d], mod - 2) % mod;
    ans += get_sum(1, 1, s[now].x - 1, 0, tm, 1, n) +
           get_sum(1, s[now].x + 1, n, 1, tm, 1, n);
    while (s[now + 1].d == s[now].d)
      now++, ans += get_sum(1, 1, s[now].x - 1, 0, tm, 1, n) +
                    get_sum(1, s[now].x + 1, n, 1, tm, 1, n);
    ans += tot * 500000004ll % mod *
           (1ll * (now - tmp + 1) * (now - tmp) / 2 % mod) % mod;
    tm = lasd[s[now].d] * nex[s[now].d + 1] % mod;
    for (int i = tmp; i <= now; i++) add(1, s[i].x, tm, 1, n);
    now++;
  }
  printf("%lld\n", ans % mod);
}
