#include <bits/stdc++.h>
using namespace std;
const int Mod = 1e9 + 7;
const int N = 1e6 + 4;
inline long long FIX(long long a) { return (a % Mod + Mod) % Mod; }
struct BIT {
  int a[N], n;
  BIT(int n) {
    Init(n);
    return;
  }
  void Init(int n) {
    this->n = n;
    memset(a, 0, sizeof(a));
    return;
  }
  void add(int pos, int val) {
    for (int i = pos; i <= n; i += i & (-i)) {
      a[i] += val;
    }
    return;
  }
  int query(int pos) {
    int res = 0;
    for (int i = pos; i >= 1; i -= i & (-i)) {
      res += a[i];
    }
    return res;
  }
};
int a[N], l[N], r[N];
long long cnt[N], sum[N], Pow[N];
long long gao(long long l, long long r) {
  return (l + r) * (r - l + 1) / 2 % Mod;
}
int main() {
  ios::sync_with_stdio();
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  BIT *bit = new BIT(n);
  cnt[0] = 0;
  long long cur = 0;
  Pow[0] = 1;
  for (int i = 1; i <= n; i++) Pow[i] = Pow[i - 1] * i % Mod;
  for (int i = 2; i <= n; i++) {
    cur = (cur + i - 1) % Mod;
    cnt[i] = cnt[i - 1] * i % Mod;
    cnt[i] += cur * Pow[i - 1] % Mod;
    cnt[i] %= Mod;
  }
  for (int i = 1; i <= n; i++) {
    l[i] = bit->query(a[i]);
    bit->add(a[i], 1);
  }
  bit->Init(n);
  for (int i = n; i >= 1; i--) {
    r[i] = bit->query(a[i]);
    bit->add(a[i], 1);
  }
  for (int i = 1; i <= n; i++) {
    sum[i] = (sum[i - 1] + r[i]) % Mod;
    sum[i] = FIX(sum[i] - (i - 1 - l[i]));
  }
  long long res = 0, cursum = 0;
  for (int i = 1; i <= n; i++) {
    long long curcnt = a[i] - 1 - l[i];
    long long num_p = curcnt * Pow[n - i] % Mod;
    res = (res + FIX(num_p * sum[i - 1])) % Mod;
    res = (res + FIX(num_p * cursum)) % Mod;
    res = (res + FIX(curcnt * cnt[n - i])) % Mod;
    if (curcnt > 0) res = (res + FIX(Pow[n - i] * gao(0, curcnt - 1))) % Mod;
    cursum = cursum + (i - 1 - l[i]);
    cursum %= Mod;
  }
  res = (res + cursum) % Mod;
  cout << res << endl;
  return 0;
}
