#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6, MOD = 1e9 + 7;
int a[N], fen[N], fac[N] = {1};
long long inv[N], ans;
int mul(int a, int b) { return 1ll * a * b % MOD; }
int Pow(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1) res = mul(res, a);
  return res;
}
void add(int idx) {
  for (; idx < N; idx += idx & (-idx)) fen[idx]++;
  return;
}
void rem(int idx) {
  for (; idx < N; idx += idx & (-idx)) fen[idx]--;
  return;
}
int get(int idx) {
  int res = 0;
  for (; idx; idx -= idx & (-idx)) res += fen[idx];
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i], fac[i] = mul(fac[i - 1], i);
  for (int i = n; i >= 1; i--) inv[i] = inv[i + 1] + get(a[i]), add(a[i]);
  for (int i = 1; i <= n; i++) {
    rem(a[i]);
    int Inv = (inv[1] - inv[i]) % MOD, Cnt = get(a[i]);
    ans += mul(mul(fac[n - i], Cnt), Inv);
    ans += mul(mul(mul(fac[n - i], Cnt), 1ll * (n - i) * (n - i - 1) / 2 % MOD),
               Pow(2, MOD - 2));
    ans += mul(fac[n - i], 1ll * Cnt * (Cnt - 1) / 2 % MOD);
    ans %= MOD;
  }
  cout << (ans + inv[1]) % MOD << "\n";
}
