#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << endl
#define _ << " _ " <<

typedef long long llint;

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 llint(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);
    //    Lc[x]--;
  }
}

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);
  REP(i, N) scanf("%d", &A[i]);

  vector<pair<int, int>> P(N);
  REP(i, N) P[i] = {A[i], i};

  sort(P.begin(), P.end());

  int total = 1;
  REP(i, N) {
    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) {
    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]);
    //    TRACE(cnt _ sum _ j);
    ret = add(ret, mul(mul(cnt, sum), inv2));
    // TRACE(ret);
    pair<int, int> below = sumv(P[i].second);
    ret = add(ret, mul(below.second, total));
    ret = sub(ret, mul(below.first, cnt));

    // TRACE(P[i].second _ ret);
    int vi = mul(F[i+1], powmod(G[i], mod-2));
    sum = add(sum, vi);
    addv(P[i].second, vi);
    // TRACE("vi" _ vi);


    if (P[i].first - i - 1 == 0) {
      while (j > i) {
        int vj = mul(F[j+1], powmod(G[j], mod-2));
        sum = sub(sum, vj);
        subv(P[j].second, vj);
        j--;
      }


    }
  }

  printf("%d\n", ret);
  return 0;
}
