#include <bits/stdc++.h>
using namespace std;
long long dp[2010000], n, fakt[2010000], zb[2010000], niz[2010000],
    bit[2020000], bit1[2020000], dp1[2010000], sol, t, poz;
void update(long long x, long long v) {
  for (; x < 1010000; x += x & -x) bit[x] += v;
}
long long read(long long x) {
  long long ret = 0;
  while (x > 0) {
    ret += bit[x];
    x -= x & -x;
  }
  return ret;
}
void update1(long long x, long long v) {
  for (; x < 1010000; x += x & -x) bit1[x] += v;
}
long long read1(long long x) {
  long long ret = 0;
  while (x > 0) {
    ret += bit1[x];
    x -= x & -x;
  }
  return ret;
}
void rj() {
  if (poz == n) return;
  t = read(niz[poz]);
  sol = (sol + zb[t - 2] * fakt[n - poz] + (t - 1) * dp[n - poz]) % 1000000007;
  sol = (sol + (t - 1) * (dp1[n - poz] + 1)) % 1000000007;
  update(niz[poz], -1);
  poz++;
  rj();
}
int main(void) {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> niz[i];
  for (int i = 1; i <= n; ++i) update(i, 1);
  for (int i = 1; i <= n; ++i) {
    zb[i] = (zb[i - 1] + i) % 1000000007;
  }
  fakt[1] = 1;
  for (int i = 2; i <= n; ++i) fakt[i] = (fakt[i - 1] * i) % 1000000007;
  dp[1] = 0;
  for (int i = 2; i <= n; ++i)
    dp[i] = (dp[i - 1] * i + zb[i - 1] * fakt[i - 1]) % 1000000007;
  for (int i = n; i > 0; --i) {
    dp1[n - i + 1] = (dp1[n - i] + read1(niz[i]) * fakt[n - i]) % 1000000007;
    update1(niz[i], 1);
  }
  poz = 1;
  rj();
  cout << sol << endl;
  return 0;
}
