#include <bits/stdc++.h>
using namespace std;
const int mo = 1000000007;
int tree[1200000], i, n, a[1200000];
long long f[1200000], tmp, ans, x, fac[1200000];
void add(int x) {
  for (int i = x; i <= n; i += i & (-i)) tree[i]++;
}
int calc(int x) {
  int res = 0;
  for (int i = x; i; i -= i & (-i)) res += tree[i];
  return res;
}
int main() {
  scanf("%d", &n);
  f[1] = 0;
  fac[1] = 1;
  for (i = 2; i <= n; ++i) {
    f[i] = (f[i - 1] * i + (1ll * i * (i - 1) / 2 % mo * fac[i - 1])) % mo;
    fac[i] = fac[i - 1] * i % mo;
  }
  for (i = 1; i <= n; ++i) scanf("%d", &a[i]);
  x = 0;
  for (i = 1; i <= n; ++i) {
    tmp = a[i] - 1 - calc(a[i] - 1);
    ans = (ans + 1ll * (x * fac[n - i] % mo + f[n - i]) * tmp +
           tmp * (tmp - 1) / 2 % mo * fac[n - i]) %
          mo;
    x = (x + tmp) % mo;
    add(a[i]);
  }
  printf("%d\n", (ans + x) % mo);
}
