#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); ++ i)
#define rrp(i,a,b) for (int i = (a); i >= (b); -- i)
#define gc() getchar()
#define fir first
#define sec second
typedef pair<int,int> pii;
typedef double db;
typedef long long ll;
template <typename tp>
inline void read(tp& x) {
  x = 0; char tmp; bool key = 0;
  for (tmp = gc(); !isdigit(tmp); tmp = gc())
    key = (tmp == '-');
  for (; isdigit(tmp); tmp = gc())
    x = (x << 3) + (x << 1) + (tmp ^ '0');
  if (key) x = -x;
}
template <typename tp>
inline void ckmn(tp& x,tp y) {
  x = x < y ? x : y;
}
template <typename tp>
inline void ckmx(tp& x,tp y) {
  x = x < y ? y : x;
}

const int MOD = (int)(1e9 + 7);
inline void Add(int& x,int y) {
  x = x + y >= MOD ? x + y - MOD : x + y;
}
inline void Sub(int& x,int y) {
  x = x - y < 0 ? x - y + MOD : x - y;
}
int power(int a,int b) {
  int ret = 1;
  while (b) {
    if (b&1) ret = 1ll * ret * a % MOD;
    a = 1ll * a * a % MOD;
    b >>= 1;
  }
  return ret;
}
const int N = 200010;
int n, val[N], cnt[N], rec[N], lp[N], rp[N], pre[N], sum, ans;
struct BIT {
#define lowbit(x) ((x) & (-(x)))
  int v[N];
  void add(int p,int val) {
    for (; p <= n; p += lowbit(p))
      Add(v[p], val);
  }
  int ask(int p) {
    int ret = 0;
    for (; p; p -= lowbit(p))
      Add(ret, v[p]);
    return ret;
  }
  int query(int l,int r) {
    int ret = ask(r);
    Sub(ret, ask(l-1));
    return ret;
  }
} b1, b2, b3;
int main() {
  read(n);
  rep (i, 1, n) read(val[i]);
  rep (i, 1, n) ++ cnt[val[i]];
  rrp (i, n-1, 1) cnt[i] += cnt[i+1];
  rep (i, 1, n) cnt[i] -= (n-i);
  sum = 1;
  rep (i, 1, n) sum = 1ll * sum * cnt[i] % MOD;
  rep (i, 1, n) rec[i] = 1ll * (cnt[i]-1) * power(cnt[i], MOD - 2) % MOD;
  for (int i = 1, j = 1; i <= n; ++ i) {
    if (rec[i] == 0) pre[i] = 1, j = i;
    else pre[i] = 1ll * pre[i-1] * rec[i] % MOD;
    lp[i] = j;
  }
  for (int i = n, j = n; i >= 1; -- i) {
    rp[i] = j;
    if (rec[i] == 0) j = i-1;
  }
  rep (i, 1, n) {
    // pre[Aj] / pre[Ai]
    int tmp = b2.query(lp[val[i]], val[i]);
    Add(ans, 1ll * tmp * pre[val[i]] % MOD * sum % MOD * (MOD + 1) / 2 % MOD);
    // sum - pre[Ai] / pre[Aj] * sum / 2
    tmp = b3.query(val[i] + 1, n);
    Add(ans, 1ll * tmp * sum % MOD);
    tmp = b1.query(val[i] + 1, rp[val[i]]);
    Sub(ans, 1ll * tmp * power(pre[val[i]], MOD-2) % MOD * sum % MOD * (MOD+1)/2 % MOD);
    b1.add(val[i], pre[val[i]]);
    b2.add(val[i], power(pre[val[i]], MOD - 2));
    b3.add(val[i], 1);
  }
  cout << ans << endl;
  return 0;
}
