#include <bits/stdc++.h>
using namespace std;
const long long Mx = 1 << 20, Inf = 1000 * 1000 * 1000 + 7;
long long n, F[Mx], S[Mx], P[Mx], D[Mx];
void add(long long i) {
  for (; i < Mx; i += i & -i) D[i]++;
}
long long get(long long i) {
  long long a = 0;
  for (; i; i -= i & -i) a += D[i];
  return a;
}
long long C2(long long x) { return (x * (x - 1) / 2) % Inf; }
pair<long long, long long> ans(long long l = 1) {
  long long p = P[l] - get(P[l]);
  if (l == n + 1) return {0, 1};
  pair<long long, long long> a = {
      (S[n - l] * (p - 1) % Inf + F[n - l] * C2(p - 1) % Inf) % Inf,
      (p - 1) * F[n - l] % Inf};
  add(P[l]);
  pair<long long, long long> x = ans(l + 1);
  return {(a.first + x.first + x.second * (p - 1) % Inf) % Inf,
          (a.second + x.second) % Inf};
}
int32_t main() {
  cin >> n;
  F[0] = F[1] = 1;
  for (long long i = 2; i <= n; i++) {
    F[i] = (F[i - 1] * i) % Inf;
    S[i] = (i * S[i - 1] % Inf + C2(i) * F[i - 1] % Inf) % Inf;
  }
  for (long long i = 1; i <= n; i++) cin >> P[i];
  cout << ans().first;
}
