#include <bits/stdc++.h>
using namespace std;
int N;
int A[202020];
int cnt[202020];
signed long long mo = 1000000007;
signed long long D[202020];
signed long long modpow(signed long long a, signed long long n = mo - 2) {
  signed long long r = 1;
  a %= mo;
  while (n) r = r * ((n % 2) ? a : 1) % mo, a = a * a % mo, n >>= 1;
  return r;
}
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> N;
  for (i = 0; i < (N); i++) {
    cin >> A[i];
    cnt[A[i]]++;
  }
  for (i = N; i >= 1; i--) cnt[i] += cnt[i + 1];
  D[0] = 1;
  signed long long S = 1;
  for (i = 1; i <= N; i++) {
    cnt[i] -= N - i;
    if (cnt[i] <= 0) return (void)printf("0\n");
    S = S * cnt[i] % mo;
    D[i] = D[i - 1] * (cnt[i] - 1) % mo * cnt[i] % mo;
  }
  signed long long ret = 0;
  for (y = 0; y < (N); y++)
    for (x = 0; x < (y); x++) {
      if (A[x] <= A[y]) {
        signed long long pat = S;
        for (i = A[x] + 1; i <= A[y]; i++)
          pat = pat * (cnt[i] - 1) % mo * modpow(cnt[i]) % mo;
        (ret += pat * ((mo + 1) / 2)) %= mo;
      } else {
        ret += mo + S;
        signed long long pat = S;
        for (i = A[y] + 1; i <= A[x]; i++)
          pat = pat * (cnt[i] - 1) % mo * modpow(cnt[i]) % mo;
        (ret -= pat * ((mo + 1) / 2) % mo) %= mo;
      }
    }
  cout << ret << endl;
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
