#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 100, OO = 1e9 + 7, M = 1e9 + 7, P = 769, sq = 360,
                lg = 26;
long long inv[N], fac[N], fen[N], n;
void upd(long long x, long long y) {
  for (; x <= n; x += (x & -x)) fen[x] = ((fen[x] + y) % M + M) % M;
}
long long get(long long x) {
  long long cnt = 0;
  for (; x; x -= (x & -x)) cnt = (cnt + fen[x]) % M;
  return cnt;
}
int32_t main() {
  cin >> n;
  long long sum = 0, ans = 0, cnt = 0;
  fac[0] = fac[1] = 1;
  upd(1, 1);
  for (long long i = 2; i <= n; i++) {
    fac[i] = (fac[i - 1] * i) % M;
    inv[i] = ((inv[i - 1] * i) % M + ((i * (i - 1) / 2) % M * fac[i - 1])) % M;
    upd(i, 1);
  }
  for (long long i = 1; i <= n; i++) {
    long long x;
    cin >> x;
    long long ls = get(x - 1), gt = (n - x) - (get(n) - get(x));
    long long g = (ls * (ls - 1) / 2);
    g = max(g, (long long)0) % M;
    ans = (ans + (ls * sum) % M * fac[n - i]) % M;
    ans = (ans + inv[n - i] * ls) % M;
    ans = (ans + g * fac[n - i]);
    ans = (ans + cnt * fac[n - i] % M * ls) % M;
    cnt = ((cnt - gt) % M + M) % M;
    cnt = (cnt + ls) % M;
    sum = (sum + gt) % M;
    upd(x, -1);
  }
  cout << (ans + sum) % M << endl;
  return 0;
}
