#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int N = 1e6 + 10;
int n, b[N], a[N];
long long fac[N], g[N];
void add(int x, int v) {
  while (x <= n) {
    b[x] += v;
    x += x & -x;
  }
}
int get(int x) {
  int ans = 0;
  while (x) {
    ans += b[x];
    x -= x & -x;
  }
  return ans;
}
void prepare() {
  scanf("%d", &n);
  for (auto i = (1); i <= (n); i++) scanf("%d", a + i);
  fac[0] = 1;
  for (auto i = (1); i <= (n); i++) fac[i] = fac[i - 1] * i % MOD;
  for (auto i = (1); i <= (n); i++)
    g[i] = (i * g[i - 1] + 1LL * i * (i - 1) / 2 % MOD * fac[i - 1]) % MOD;
}
long long solve() {
  long long ans = 0, sum = 0;
  for (auto i = (1); i <= (n); i++) {
    int pos = a[i] - get(a[i]);
    ans = (ans + (pos - 1) * g[n - i] + sum * (pos - 1) % MOD * fac[n - i] +
           1LL * (pos - 2) * (pos - 1) / 2 % MOD * fac[n - i]) %
          MOD;
    sum = (sum + pos - 1) % MOD;
    add(a[i], 1);
  }
  return (ans + sum) % MOD;
}
int main() {
  prepare();
  cout << solve();
}
