#include <bits/stdc++.h>
int read() {
  int r = 0, t = 1, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = r * 10 + c - 48;
    c = getchar();
  }
  return r * t;
}
const int N = 200010, mod = 1000000007, i2 = (mod + 1) / 2;
long long n, a[N], b[N], ans, A[N];
long long solve(long long *a) {
  long long ans = 1;
  for (int i = (1), end_i = (n); i <= end_i; i++) b[i] = a[i];
  std::sort(b + 1, b + n + 1);
  for (int i = (1), end_i = (n); i <= end_i; i++)
    ans = ans * (b[i] - i + 1) % mod;
  return ans;
}
int main() {
  n = read();
  for (int i = (1), end_i = (n); i <= end_i; i++) A[i] = a[i] = read();
  for (int i = (1), end_i = (n); i <= end_i; i++)
    for (int j = (i + 1), end_j = (n); j <= end_j; j++)
      if (a[i] == a[j])
        ans = (ans + solve(a) * i2) % mod;
      else if (a[i] > a[j]) {
        ans = (ans + solve(a)) % mod;
        A[i] = a[j];
        ans = (ans - solve(A) * i2) % mod;
        A[i] = a[i];
      } else {
        A[j] = a[i];
        ans = (ans + solve(A) * i2) % mod;
        A[j] = a[j];
      }
  printf("%lld\n", (ans % mod + mod) % mod);
  return 0;
}
