#include <bits/stdc++.h>
using namespace std;
template <class T>
inline int size(const T& c) {
  return c.size();
}
using namespace std;
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }
const int cmod = 1000000007;
const int maxn = 1000000 + 10;
int n;
int a[maxn];
int F[maxn];
long long res;
long long sum;
int b[2][maxn];
long long gt[maxn];
void preprocessing() {
  F[0] = 0;
  gt[0] = 1;
  for (int i = 1; i <= n; i++) {
    gt[i] = (gt[i - 1] * i) % cmod;
    F[i] = (F[i] + (long long)i * F[i - 1] +
            ((long long)(i - 1) * (i) / 2) % cmod * gt[i - 1]) %
           cmod;
  }
}
int get(int k, int u) {
  int res = 0;
  for (int i = u; i >= 1; i -= (i & (-i))) res = (res + b[k][i]) % cmod;
  return res;
}
void update(int k, int u, int gt) {
  if (u == 0) return;
  for (int i = u; i <= n; i += (i & (-i))) b[k][i] = b[k][i] + gt;
}
void process() {
  long long sum = 0;
  long long sfront = 0;
  res = 0;
  for (int i = 1; i <= n; i++) update(0, i, 1);
  for (int i = 1; i <= n; i++) {
    long long sl = get(0, a[i] - 1);
    res = (res + (long long)gt[n - i] * (((sl - 1) * sl / 2) % cmod)) % cmod;
    res = (res +
           (long long)sl * (((sfront + sum) * gt[n - i]) % cmod + F[n - i])) %
          cmod;
    sum = (sum - (get(1, n) - get(1, a[i]))) % cmod;
    sum = (sum + get(0, a[i] - 1)) % cmod;
    update(1, a[i], 1);
    update(0, a[i], -1);
    sfront = (sfront + (get(1, n) - get(1, a[i]))) % cmod;
  }
  res = (res + sfront) % cmod;
  cout << (res + cmod) % cmod << endl;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  preprocessing();
  process();
  fclose(stdin);
  fclose(stdout);
  return 0;
}
