#include <bits/stdc++.h>
using namespace std;
const int mo = 1e9 + 7;
long long ksm(long long x, long long y) {
  long long s = 1;
  for (; y; y /= 2, x = x * x % mo)
    if (y & 1) s = s * x % mo;
  return s;
}
const long long ni2 = ksm(2, mo - 2);
const int N = 2e5 + 5;
int n, a[N], d[N];
int cnt[N];
long long b[N], c[N];
int cmp(int x, int y) {
  if (a[x] == a[y]) return x < y;
  return a[x] < a[y];
}
long long w[N];
long long sum, ans;
void dg(int x, int y, int u, int v) {
  if (u >= v) return;
  if (x == y) {
    ans = (ans + sum * (v - u + 1) % mo * (v - u) % mo * ni2 % mo * ni2) % mo;
    return;
  }
  int mi = x + y >> 1, m = u - 1;
  while (m < v && a[d[m + 1]] <= mi) m++;
  dg(x, mi, u, m);
  dg(mi + 1, y, m + 1, v);
  w[mi] = 1;
  for (int i = mi - 1, B = x; i >= B; i--) w[i] = w[i + 1] * c[i + 1] % mo;
  w[mi + 1] = c[mi + 1];
  for (int i = mi + 2, B = y; i <= B; i++) w[i] = w[i - 1] * c[i] % mo;
  int l = u;
  long long s = 0;
  for (int i = m + 1, B = v; i <= B; i++) {
    while (l <= m && d[l] <= d[i]) s = (s + w[a[d[l]]]) % mo, l++;
    ans = (ans + sum * s % mo * w[a[d[i]]] % mo * ni2) % mo;
  }
  l = m, s = 0;
  for (int i = v, B = m + 1; i >= B; i--) {
    while (l >= u && d[l] > d[i]) s = (s + w[a[d[l]]]) % mo, l--;
    ans = (ans + sum * (m - l) - sum * s % mo * w[a[d[i]]] % mo * ni2) % mo;
  }
  sort(d + u, d + v + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1, B = n; i <= B; i++) {
    scanf("%d", &a[i]);
    if (a[i] > n) a[i] = n;
    cnt[a[i]]++;
  }
  for (int i = n, B = 1; i >= B; i--) cnt[i] += cnt[i + 1];
  sum = 1;
  for (int i = 1, B = n; i <= B; i++) {
    b[i] = cnt[i] - n + i;
    sum = sum * max(0ll, b[i]) % mo;
  }
  for (int i = 1, B = n; i <= B; i++) {
    c[i] = ksm(b[i], mo - 2) * (b[i] - 1) % mo;
  }
  for (int i = 1, B = n; i <= B; i++) d[i] = i;
  sort(d + 1, d + n + 1, cmp);
  dg(1, n, 1, n);
  ans = (ans % mo + mo) % mo;
  printf("%lld\n", ans);
}
