#include <bits/stdc++.h>
using namespace std;
long long po(long long x, long long y) {
  long long z = 1;
  while (y) {
    if (y % 2) z = x * z % 1000000007;
    x = x * x % 1000000007;
    y /= 2;
  }
  return z;
}
struct fs {
  long long x;
  int y;
} q[200010], p[200010];
fs operator*(fs x, long long y) {
  if (y == 0)
    x.y++;
  else
    x.x = x.x * y % 1000000007;
  return x;
}
fs operator*(fs x, fs y) {
  fs z;
  z.y = x.y + y.y;
  z.x = x.x * y.x % 1000000007;
  return z;
}
fs operator/(fs x, fs y) {
  fs z;
  z.y = x.y - y.y;
  z.x = x.x * po(y.x, 1000000007 - 2) % 1000000007;
  return z;
}
long long zh(fs x) {
  if (x.y == 0) return x.x;
  return 0;
}
long long sum, n, a[200010], g[200010], ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    g[a[i]]++;
  }
  for (int i = n; i >= 1; i--) g[i] += g[i + 1];
  for (int i = 1; i <= n; i++) g[i] = g[i] - n + i;
  for (int i = 1; i <= n; i++)
    if (g[i] <= 0) {
      puts("0");
      return 0;
    }
  q[0] = (fs){1, 0};
  for (int i = 1; i <= n; i++) q[i] = q[i - 1] * g[i];
  p[0] = (fs){1, 0};
  for (int i = 1; i <= n; i++) p[i] = p[i - 1] * (g[i] - 1);
  for (int i = 1; i <= n; i++) p[i] = p[i] / q[i];
  sum = q[n].x;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (a[i] <= a[j]) {
        ans = (ans + (sum * zh(p[a[j]] / p[a[i]]) % 1000000007) *
                         po(2, 1000000007 - 2)) %
              1000000007;
      } else {
        ans = (ans + sum * (1 + 1000000007 -
                            zh(p[a[i]] / p[a[j]]) * po(2, 1000000007 - 2) %
                                1000000007)) %
              1000000007;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
