#include <bits/stdc++.h>
using namespace std;
struct aib {
  int n;
  vector<long long> data;
  void init(int _n) {
    n = _n;
    data = vector<long long>(n + 13, 0);
  }
  int zrs(int x) { return (x & (x - 1)) ^ x; }
  void add(int x, long long val) {
    while (x <= n) {
      data[x] += val;
      x += zrs(x);
    }
  }
  long long sum(int x) {
    long long ans = 0;
    while (x > 0) {
      ans += data[x];
      x -= zrs(x);
    }
    return ans;
  }
};
int n, i;
int p[1000011];
long long fact[1000011], inv_fact[1000011];
aib work;
long long ans, rez;
long long poww(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) ans = (ans * a) % 1000000007;
    b >>= 1;
    a = (a * a) % 1000000007;
  }
  return ans;
}
void pre() {
  long long i, j;
  fact[0] = 1;
  for (i = 1; i <= n; i++) fact[i] = (fact[i - 1] * i) % 1000000007;
  inv_fact[n] = poww(fact[n], 1000000007 - 2);
  for (i = n - 1; i > 0; i--)
    inv_fact[i] = (inv_fact[i + 1] * (i + 1)) % 1000000007;
  inv_fact[0] = 1;
}
long long formula(long long n) {
  if (n <= 1) return 0;
  long long ans = (fact[n] * inv_fact[2]) % 1000000007;
  ans = (ans * inv_fact[2]) % 1000000007;
  ans = (ans * n) % 1000000007;
  ans = (ans * (n - 1)) % 1000000007;
  return ans;
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &p[i]);
  pre();
  work.init(n);
  if (next_permutation(p + 1, p + n + 1) == false) {
    printf("%lld", formula(n));
    return 0;
  }
  for (i = 1; i <= n; i++) {
    long long cnt = p[i] - 1 - work.sum(p[i] - 1);
    work.add(p[i], +1);
    ans += (cnt * formula(n - i)) % 1000000007;
    ans += ((((cnt * (cnt - 1)) / 2) % 1000000007) * fact[n - i]) % 1000000007;
    ans %= 1000000007;
    ans += (((rez * fact[n - i]) % 1000000007) * cnt) % 1000000007;
    ans %= 1000000007;
    rez += cnt;
    rez %= 1000000007;
  }
  printf("%lld", ans);
  return 0;
}
