#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target( \
    "avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long MOD = 1e9 + 7;
const int N = 1200000;
long long fac[N];
long long fn[N];
long long st(long long t, long long id, long long mod) {
  long long res = 1, a = t, n = id;
  while (n) {
    if (n & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    n >>= 1;
  }
  return res % mod;
}
long long del(long long a, long long b) {
  return (a * st(b, MOD - 2, MOD)) % MOD;
}
long long tr[N];
void inc(long long i, long long x) {
  for (; i < N; i = ((i + 1) | i)) {
    tr[i] += x;
  }
}
long long sm(long long i) {
  long long ans = 0;
  for (; i > -1; i = ((i + 1) & i) - 1) {
    ans += tr[i];
  }
  return ans;
}
long long i2 = del(1, 2);
pair<long long, long long> otv(vector<long long> &p) {
  if (p.size() == 1) {
    return {0, 1};
  }
  long long n = p.size();
  long long k = sm(p[n - 1]) - 1;
  inc(p[n - 1], -1);
  p.pop_back();
  long long ans =
      fn[n - 1] * k % MOD + fac[n - 1] * k % MOD * (k - 1) % MOD * i2 % MOD;
  ans %= MOD;
  auto tmp = otv(p);
  ans += tmp.first;
  ans += k * tmp.second % MOD;
  ans %= MOD;
  return {ans, (tmp.second + k * fac[n - 1]) % MOD};
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
      cout << fixed << setprecision(20);
  fac[0] = 1;
  for (int i = 1; i < N; i++) {
    fac[i] = fac[i - 1] * i % MOD;
  }
  fn[1] = 0;
  for (int i = 2; i < N; i++) {
    fn[i] = fn[i - 1] * i % MOD + fac[i] * (i - 1) % MOD * i2 % MOD;
    fn[i] %= MOD;
  }
  long long n;
  cin >> n;
  vector<long long> p(n);
  for (int i = 0; i < n; i++) {
    inc(i, 1);
  }
  for (int i = 0; i < n; i++) {
    cin >> p[n - i - 1];
    p[n - i - 1]--;
  }
  cout << otv(p).first;
}
