#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int blah4 = 250000002;
const int blah2 = 500000004;
const int upper = 1000010;
int fact[upper], a[upper], b[upper], num[upper], p[upper], q[upper];
void calfact() {
  long long f = 1, i;
  fact[0] = 1;
  for (i = 1; i < upper; i++) {
    f = (f * i) % mod;
    fact[i] = f;
  }
}
void merge(int k, int l, int m, int n) {
  int i, j, u, v, c;
  for (i = k, j = 0; i <= l; i++, j++) {
    p[j] = b[i];
  }
  u = j;
  for (i = m, j = 0; i <= n; i++, j++) {
    q[j] = b[i];
  }
  v = j;
  i = 0;
  j = 0;
  c = k;
  while (i < u && j < v) {
    if (p[i] <= q[j]) {
      b[c] = p[i];
      num[p[i]] += j;
      i++;
    } else {
      b[c] = q[j];
      j++;
    }
    c++;
  }
  if (i == u) {
    for (; j < v; j++, c++) {
      b[c] = q[j];
    }
  } else {
    for (; i < u; i++, c++) {
      b[c] = p[i];
      num[p[i]] += j;
    }
  }
}
void mergesort(int a, int b) {
  int m;
  if (a == b) {
    return;
  }
  m = (a + b) / 2;
  mergesort(a, m);
  mergesort(m + 1, b);
  merge(a, m, m + 1, b);
}
int main() {
  int i, n, j;
  calfact();
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
    num[i] = 0;
  }
  int ans = 0;
  mergesort(1, n);
  for (i = 1; i <= n; i++) {
    b[i] = num[a[i]];
  }
  int f = 0, g = 1;
  long long f1, g1;
  for (i = 1, j = n - 1; i < n; i++, j--) {
    f1 = fact[i];
    f1 = (((f1 * i) % mod * (i - 1)) % mod * blah4) % mod;
    g1 = fact[i];
    f1 = (((b[j] * (g + f1)) % mod +
           (((g1 * blah2) % mod * (b[j] - 1)) % mod * b[j]) % mod) %
              mod +
          f) %
         mod;
    g1 = ((b[j] * g1) % mod + g) % mod;
    f = f1;
    g = g1;
  }
  cout << f << endl;
}
