#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int SIZE = 1e6 + 10;
long long dp[SIZE], fac[SIZE], dp2[SIZE];
int BIT[SIZE], a[SIZE];
void ins(int x) {
  while (x < SIZE) {
    BIT[x]++;
    x += x & -x;
  }
}
int qq(int x) {
  int res = 0;
  while (x) {
    res += BIT[x];
    x -= x & -x;
  }
  return res;
}
int main() {
  int(n);
  scanf("%d", &n);
  dp[1] = 0;
  fac[0] = 1;
  for (int i = (1); i < (n + 1); ++i) fac[i] = fac[i - 1] * i % MOD;
  for (int i = (2); i < (n + 1); ++i) {
    dp[i] = dp[i - 1] * i % MOD +
            fac[i - 1] * (((long long)i * (i - 1) / 2) % MOD) % MOD;
    dp[i] %= MOD;
  }
  for (int i = 0; i < (n); ++i) scanf("%d", &(a[i]));
  ins(a[n - 1]);
  dp2[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--) {
    int x = a[i];
    int now = qq(x);
    ins(x);
    dp2[i] = now * fac[n - i - 1] + dp2[i + 1];
    dp2[i] %= MOD;
  }
  memset((BIT), 0, sizeof((BIT)));
  long long an = 0;
  for (int i = 0; i < (n); ++i) {
    int x = a[i];
    ins(x);
    int now = x - qq(x);
    an += dp[n - i - 1] * now % MOD;
    an %= MOD;
    an += ((long long)now * (now - 1) / 2) % MOD * fac[n - i - 1] % MOD;
    an %= MOD;
    an += (dp2[i] - now * fac[n - i - 1] % MOD) * now;
    an %= MOD;
    if (an < 0) an += MOD;
  }
  cout << an << endl;
  return 0;
}
