#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 200010;
int n, C = 1, ans = 0;
pair<int, int> a[maxn];
int b[maxn], s[maxn], sum[maxn << 2], cnt[maxn << 2];
int qpow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1) ret = 1LL * ret * x % mod;
    x = 1LL * x * x % mod;
    y >>= 1;
  }
  return ret;
}
void pushUp(int rt) {
  sum[rt] = (sum[rt << 1] + sum[rt << 1 | 1]) % mod;
  cnt[rt] = cnt[rt << 1] + cnt[rt << 1 | 1];
}
void update(int p, int v, int l, int r, int rt) {
  if (l == r) {
    sum[rt] = (sum[rt] + v) % mod;
    cnt[rt]++;
    return;
  }
  int m = (l + r) >> 1;
  if (p <= m)
    update(p, v, l, m, rt << 1);
  else
    update(p, v, m + 1, r, rt << 1 | 1);
  pushUp(rt);
}
int query(int L, int R, int l, int r, int rt) {
  if (L > R) return 0;
  if (L <= l && r <= R) return sum[rt];
  int ret = 0;
  int m = (l + r) >> 1;
  if (L <= m) ret = (ret + query(L, R, l, m, rt << 1)) % mod;
  if (R > m) ret = (ret + query(L, R, m + 1, r, rt << 1 | 1)) % mod;
  return ret;
}
int query_cnt(int L, int R, int l, int r, int rt) {
  if (L > R) return 0;
  if (L <= l && r <= R) return cnt[rt];
  int ret = 0;
  int m = (l + r) >> 1;
  if (L <= m) ret = ret + query_cnt(L, R, l, m, rt << 1);
  if (R > m) ret = ret + query_cnt(L, R, m + 1, r, rt << 1 | 1);
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i].first);
    a[i].second = i;
  }
  sort(a + 1, a + n + 1);
  s[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i].first - i + 1 <= 0) {
      puts("0");
      return 0;
    }
    b[i] = 1LL * (a[i].first - i) * qpow(a[i].first - i + 1, mod - 2) % mod;
  }
  for (int i = 1; i <= n; i++) s[i] = 1LL * s[i - 1] * b[i] % mod;
  for (int i = 1; i <= n; i++) C = 1LL * C * (a[i].first - i + 1) % mod;
  for (int i = 1; i <= n; i++) {
    int v = 1LL * (mod + 1) / 2 * C % mod * s[i - 1] % mod *
            qpow(a[i].first - i + 1, mod - 2) % mod;
    ans = (ans + 1LL * v * query(1, a[i].second - 1, 1, n, 1) % mod) % mod;
    ans =
        ((ans + 1LL * C * query_cnt(a[i].second + 1, n, 1, n, 1) % mod) % mod +
         mod - 1LL * v * query(a[i].second + 1, n, 1, n, 1) % mod) %
        mod;
    update(a[i].second, 1LL * (a[i].first - i) * qpow(s[i], mod - 2) % mod, 1,
           n, 1);
  }
  printf("%d\n", ans);
  return 0;
}
