#include <bits/stdc++.h>
using namespace std;
template <typename TYPE>
inline void chkmax(TYPE &first, TYPE second) {
  first < second ? first = second : 0;
}
template <typename TYPE>
inline void chkmin(TYPE &first, TYPE second) {
  second < first ? first = second : 0;
}
template <typename TYPE>
void readint(TYPE &first) {
  first = 0;
  int f = 1;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) first = first * 10 + c - '0';
  first *= f;
}
const int MAXN = 200005, MOD = 1000000007, INV2 = (MOD + 1) >> 1;
int qmi(int first, int second) {
  int ans = 1;
  for (; second; second >>= 1, first = 1ll * first * first % MOD)
    if (second & 1) ans = 1ll * ans * first % MOD;
  return ans;
}
inline int dmy(int first) { return first >= MOD ? first - MOD : first; }
int n, a[MAXN], b[MAXN], p[MAXN], las[MAXN], invp[MAXN];
struct Fenwick {
  int c[MAXN];
  void add(int first, int d) {
    for (; first <= n; first += (first & -first)) c[first] = dmy(c[first] + d);
  }
  int query(int first) {
    int s = 0;
    for (; first; first -= (first & -first)) s = dmy(s + c[first]);
    return s;
  }
} c0, c1, c2;
int main() {
  int sum = INV2, ans = 0;
  readint(n);
  for (int i = 1; i <= n; ++i) readint(b[i]), ++a[b[i]];
  for (int i = n; i; --i) a[i] += a[i + 1];
  p[0] = 1;
  for (int i = 1; i <= n; ++i) {
    a[i] -= (n - i);
    if (a[i] <= 0) return 0 * printf("0\n");
    if (a[i - 1] > 1)
      p[i] = 1ll * p[i - 1] * (a[i] - 1) % MOD * qmi(a[i], MOD - 2) % MOD;
    else
      p[i] = 1ll * (a[i] - 1) * qmi(a[i], MOD - 2) % MOD;
    if (a[i] == 1)
      las[i] = i;
    else
      las[i] = las[i - 1];
    invp[i] = qmi(p[i], MOD - 2);
    sum = 1ll * sum * a[i] % MOD;
  }
  for (int i = 1; i <= n; ++i) {
    ans = (ans + 1ll * (c0.query(b[i]) - c0.query(las[b[i]]) + MOD) * sum %
                     MOD * p[b[i]]) %
          MOD;
    ans = (ans + 1ll * (c2.query(b[i]) - c2.query(n) + MOD) * sum % MOD *
                     invp[b[i]]) %
          MOD;
    ans = (ans + 2ll * (c1.query(n) - c1.query(b[i]) + MOD) % MOD * sum) % MOD;
    c0.add(b[i], invp[b[i]]);
    c1.add(b[i], 1);
    c2.add(b[i], p[b[i]]);
  }
  printf("%d\n", ans);
  return 0;
}
