#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inv2 = (mod + 1) / 2;
inline int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int sub(int a, int b) { return a >= b ? a - b : a - b + mod; }
inline int mul(int a, int b) { return long long(a) * b % mod; }
int powmod(int a, int b) {
  if (b == 0) return 1;
  if (b & 1) return mul(a, powmod(a, b - 1));
  return powmod(mul(a, a), b / 2);
}
const int MAX = 200100;
int L[MAX], Lc[MAX];
void addv(int x, int v) {
  for (++x; x < MAX; x += x & -x) {
    L[x] = add(L[x], v);
    Lc[x]++;
  }
}
void subv(int x, int v) {
  for (++x; x < MAX; x += x & -x) {
    L[x] = sub(L[x], v);
  }
}
pair<int, int> sumv(int x) {
  int r = 0;
  int rc = 0;
  for (++x; x; x -= x & -x) {
    r = add(r, L[x]);
    rc += Lc[x];
  }
  return {r, rc};
}
int main(void) {
  int N;
  scanf("%d", &N);
  vector<int> A(N);
  for (int i = (0); i < (N); ++i) scanf("%d", &A[i]);
  vector<pair<int, int>> P(N);
  for (int i = (0); i < (N); ++i) P[i] = {A[i], i};
  sort(P.begin(), P.end());
  int total = 1;
  for (int i = (0); i < (N); ++i) {
    if (P[i].first - i > 0) {
      total = mul(total, P[i].first - i);
    } else {
      printf("0\n");
      return 0;
    }
  }
  vector<int> F(N + 1), G(N + 1);
  F[N] = G[N] = 1;
  for (int i = N - 1; i >= 0; --i) {
    F[i] = mul(F[i + 1], (P[i].first - i));
    G[i] = mul(G[i + 1], max(1, P[i].first - i - 1));
  }
  int ret = 0;
  int j = N - 1;
  int sum = 0;
  for (int i = N - 1; i >= 0; --i) {
    while (j > i + 1 && P[j - 1].first - (j - 1) - 1 == 0) {
      int vj = mul(F[j + 1], powmod(G[j], mod - 2));
      sum = sub(sum, vj);
      subv(P[j].second, vj);
      j--;
    }
    int cnt = P[i].first - i - 1;
    cnt = mul(cnt, mul(F[0], powmod(F[i + 1], mod - 2)));
    cnt = mul(cnt, G[i + 1]);
    ret = add(ret, mul(mul(cnt, sum), inv2));
    pair<int, int> below = sumv(P[i].second);
    ret = add(ret, mul(below.second, total));
    ret = sub(ret, mul(below.first, cnt));
    int vi = mul(F[i + 1], powmod(G[i], mod - 2));
    sum = add(sum, vi);
    addv(P[i].second, vi);
  }
  printf("%d\n", ret);
  return 0;
}
