#include <bits/stdc++.h>
int n, F[1000009];
void add(int p) {
  for (; p <= n; p += p & -p) F[p]++;
}
int sum(int p) {
  int v = 0;
  for (; p; p -= p & -p) v += F[p];
  return v;
}
long long ans, f[1000009], a[1000009];
int main() {
  scanf("%d", &n);
  f[0] = 1;
  for (int i = 1; i <= n; i++) f[i] = f[i - 1] * i % 1000000007;
  a[0] = 0;
  for (long long i = 1; i <= n; i++)
    a[i] =
        (a[i - 1] * i + i * (i - 1) / 2 % 1000000007 * f[i - 1]) % 1000000007;
  long long inv = 0;
  for (int i = 1, p; i <= n; i++) {
    scanf("%d", &p);
    long long q = p - sum(p);
    ans = (ans + f[n - i] * (q - 1) % 1000000007 * inv) % 1000000007;
    ans = (ans + (q - 1) * (q - 2) / 2 % 1000000007 * f[n - i]) % 1000000007;
    ans = (ans + a[n - i] * (q - 1)) % 1000000007;
    add(p);
    inv = (inv + q - 1) % 1000000007;
  }
  ans = (ans + inv) % 1000000007;
  printf("%I64d\n", ans);
}
