#include <bits/stdc++.h>
using namespace std;
const int q = 1000000007;
int n, a[200010], f[200010], p, r = 1;
inline int power(int a, int b) {
  if (!b) return 1;
  int c = power(a, b >> 1);
  c = (long long)c * c % q;
  if (b & 1) c = (long long)c * a % q;
  return c;
}
int main() {
  int i, j, k, l, u, v;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = n; i > 0; i--) {
    for (j = 1, k = -(n - i), u = 0; j <= n; j++) {
      if (a[j] >= i) k++;
      if (a[j] == i) u++;
    }
    l = power(k, q - 2);
    for (j = 1; j <= n; j++)
      if (a[j] == i)
        for (v = 1; v < j; v++)
          if (a[v] > i) p = (p + f[v]) % q;
    p = (p + ((long long)u * (u - 1) / 2 + (long long)u * (k - u)) % q *
                 500000004ll) %
        q;
    r = (long long)r * k % q;
    for (j = 1; j <= n; j++)
      if (a[j] >= i) f[j] = (f[j] + (long long)(1 - f[j]) * l) % q;
  }
  p = (long long)p * r % q;
  p = (p + q) % q;
  printf("%d\n", p);
  return 0;
}
