#include <bits/stdc++.h>
using namespace std;
int n;
int r[1000009];
long long int fac[1000009];
long long int fac2[1000009];
long long int perm[1000009];
long long int ans[1000009];
class Fw {
 public:
  Fw(int n) { this->r_.assign(n, 0); }
  void Add(int k, int value) {
    while (k < r_.size()) {
      r_[k] += value;
      k = k | (k + 1);
    }
  }
  long long int Get(int k) {
    long long int ans = 0;
    while (k >= 0) {
      ans += r_[k];
      k = (k & (k + 1)) - 1;
    }
    return ans;
  }

 private:
  vector<long long int> r_;
};
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &(r[i]));
  }
  fac[1] = 1;
  for (long long int i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= 1000000007LL;
  }
  fac2[1] = 0;
  fac2[2] = 1;
  for (long long int i = 3; i <= n; i++) {
    fac2[i] = fac2[i - 1] * i;
    fac2[i] %= 1000000007LL;
  }
  perm[n - 1] = 1;
  ans[n - 1] = 0;
  Fw fw(n);
  fw.Add(r[n - 1], 1);
  for (long long int i = n - 2; i >= 0; i--) {
    perm[i] = perm[i + 1] + fac[n - i - 1] * fw.Get(r[i] - 1);
    perm[i] %= 1000000007LL;
    ans[i] = ans[i + 1] + ((perm[i + 1] * fw.Get(r[i] - 1)) % 1000000007LL);
    ans[i] %= 1000000007LL;
    long long int z2 = ((n - i - 1) * (n - i - 2) / 2) % 1000000007LL;
    long long int z = (fac2[n - i - 1] * z2) % 1000000007LL;
    long long int k = fw.Get(r[i] - 1);
    ans[i] += ((z * k) % 1000000007LL);
    ans[i] %= 1000000007LL;
    long long int kk = (k * (k - 1) / 2) % 1000000007LL;
    ans[i] += fac[n - i - 1] * kk;
    ans[i] %= 1000000007LL;
    fw.Add(r[i] - 1, 1);
  }
  cout << ans[0] << endl;
  return 0;
}
