#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chkmax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
inline void chkmin(T &a, T b) {
  if (a > b) a = b;
}
inline int read() {
  int sum = 0, p = 1;
  char ch = getchar();
  while (!(('0' <= ch && ch <= '9') || ch == '-')) ch = getchar();
  if (ch == '-') p = -1, ch = getchar();
  while ('0' <= ch && ch <= '9') sum = sum * 10 + ch - 48, ch = getchar();
  return sum * p;
}
const int maxn = 1e5 + 20;
const int mod = 1e9 + 7;
inline void pls(int &a, int b) {
  a += b;
  if (a >= mod) a -= mod;
}
inline int power(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = (long long)ans * a % mod;
    b >>= 1;
    a = (long long)a * a % mod;
  }
  return ans;
}
int n, a[maxn], id[maxn];
int inv[maxn];
int S = 1;
int pre[maxn], suf[maxn];
pair<int, int> Pre[maxn], Suf[maxn];
inline pair<int, int> operator*(pair<int, int> a, pair<int, int> b) {
  return make_pair((long long)a.first * b.first % mod, a.second + b.second);
}
inline pair<int, int> operator/(pair<int, int> a, pair<int, int> b) {
  return make_pair((long long)a.first * power(b.first, mod - 2) % mod,
                   a.second - b.second);
}
inline pair<int, int> Num(int x) {
  return !x ? make_pair(0, 1) : make_pair(x, 0);
}
inline void init() {
  n = read();
  for (int i = (1), _end_ = (n); i <= _end_; i++) a[i] = read(), id[i] = i;
  sort(id + 1, id + n + 1, [](int x, int y) { return a[x] < a[y]; });
  for (int i = (1), _end_ = (n); i <= _end_; i++)
    S = (long long)S * (a[id[i]] - i + 1) % mod;
  if (!S) puts("0"), exit(0);
  inv[1] = 1;
  for (int i = (2), _end_ = (n); i <= _end_; i++)
    inv[i] = (long long)(mod - mod / i) * inv[mod % i] % mod;
  pre[0] = 1;
  for (int i = (1), _end_ = (n); i <= _end_; i++)
    pre[i] = (long long)pre[i - 1] * (a[id[i]] - i + 1) % mod;
  suf[n + 1] = 1;
  for (int i = (n), _end_ = (1); i >= _end_; i--)
    suf[i] = (long long)suf[i + 1] * (a[id[i]] - i + 1) % mod;
  Pre[0] = make_pair(1, 0);
  for (int i = (1), _end_ = (n); i <= _end_; i++)
    Pre[i] = Pre[i - 1] * Num(a[id[i]] - i + 1);
  Suf[n + 1] = make_pair(1, 0);
  for (int i = (n), _end_ = (1); i >= _end_; i--)
    Suf[i] = Suf[i + 1] * Num(a[id[i]] - i + 1);
}
int s[maxn], vis[maxn], tim;
inline void Add(int x, int d, int Rev) {
  for (x = Rev ? n - x + 1 : x; x <= n; x += x & (-x))
    vis[x] == tim ? (pls(s[x], d), 0) : (vis[x] = tim, s[x] = d);
}
inline int Sum(int x, int Rev) {
  int res = 0;
  for (x = Rev ? n - x + 1 : x; x; x -= x & (-x))
    (vis[x] != tim ? (vis[x] = tim, s[x] = 0) : (pls(res, s[x]), 0));
  return res;
}
int ans;
inline void doing() {
  int de = 1;
  for (int i = (n), _end_ = (1); i >= _end_; i--) {
    int u = id[i];
    int t = Sum(u + 1, 1);
    pls(ans, (long long)pre[i - 1] * de % mod * t % mod *
                 (((long long)(a[u] - i + 1) * (a[u] - i) >> 1) % mod) % mod);
    if (a[u] - i)
      de = (long long)de * (a[u] - i) % mod;
    else
      de = 1, ++tim;
    Add(u, (long long)suf[i + 1] * power(de, mod - 2) % mod, 1);
  }
  ++tim;
  for (int i = (n), _end_ = (1); i >= _end_; i--) {
    int u = id[i];
    int t = Sum(u - 1, 0);
    pls(ans, (long long)t * pre[n] % mod);
    Add(u, 1, 0);
  }
  ++tim;
  de = 1;
  for (int i = (n), _end_ = (1); i >= _end_; i--) {
    int u = id[i];
    int t = Sum(u - 1, 0);
    pls(ans, mod - (long long)pre[i - 1] * de % mod * t % mod *
                       (((long long)(a[u] - i + 1) * (a[u] - i) >> 1) % mod) %
                       mod);
    if (a[u] - i)
      de = (long long)de * (a[u] - i) % mod;
    else
      de = 1, ++tim;
    Add(u, (long long)suf[i + 1] * power(de, mod - 2) % mod, 0);
  }
  printf("%d\n", ans);
}
int main() {
  init();
  doing();
  return 0;
}
