#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const long long linf = 1e18 + 5;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
int n, a[N], fen[N];
long long fact[N], dp[N], res[N], small_on[N];
long long sum(long long x) { return ((long long)x * (x + 1) / 2) % mod; }
void up(int x, int k) {
  for (; x < N; x += x & (-x)) fen[x] += k;
}
int qu(int x) {
  int sum = 0;
  for (; x; x -= x & (-x)) sum += fen[x];
  return sum;
}
int main() {
  ios ::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) up(i, 1);
  fact[0] = 1;
  for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % mod;
  for (int i = 1; i <= n; i++)
    dp[i] = (dp[i - 1] * i + sum(i - 1) * fact[i - 1]) % mod;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int small = qu(a[i] - 1);
    ans = (ans + small * dp[n - i] + sum(small - 1) * fact[n - i]) % mod;
    small_on[i] = small;
    ans = (ans + small * res[i + 1]) % mod;
    up(a[i], -1);
  }
  res[n] = 1;
  for (int i = n - 1; i >= 1; i--)
    res[i] = (small_on[i] * fact[n - i] + res[i + 1]) % mod;
  for (int i = 1; i <= n; i++) ans = (ans + small_on[i] * res[i + 1]) % mod;
  cout << ans << '\n';
  return 0;
}
