#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int mo = 1000000007;
int cnt[N], a[N];
int val1[N], val2[N];
int val3[N], val4[N];
int vpr[N], vsu[N];
int pre[N], suf[N];
int ifac[N], iinv[N];
int n, ans, V, S[N];
int power(int x, int y) {
  int s = 1;
  for (; y; y /= 2, x = 1ll * x * x % mo)
    if (y & 1) s = 1ll * s * x % mo;
  return s;
}
int func(int l, int r) {
  if (pre[r - 1] >= l + 1) return 0;
  return 1ll * vpr[l - 1] * vsu[r + 1] % mo * val4[l] % mo * val3[r] % mo *
         ifac[r - 1] % mo * iinv[l] % mo;
}
struct TARR {
  int t[N];
  void change(int x, int v) {
    for (; x <= n; x += x & (-x)) t[x] = (t[x] + v) % mo;
  }
  int ask(int x) {
    long long ans = 0;
    for (; x; x -= x & (-x)) ans += t[x];
    return ans % mo;
  }
  int ask(int l, int r) {
    l = max(l, 1);
    r = min(r, n);
    if (l > r) return 0;
    return (ask(r) + mo - ask(l - 1)) % mo;
  }
} T1, T2, T3;
void update(int x, int y) {
  if (a[x] > a[y]) ans = (ans + mo - func(a[y], a[x])) % mo;
  if (a[x] < a[y]) ans = (ans + func(a[x], a[y])) % mo;
}
int main() {
  scanf("%d", &n);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &a[i]), cnt[a[i]]++;
  for (int i = (int)(1); i <= (int)(n); i++) S[i] = S[i - 1] + cnt[i];
  V = 1;
  for (int i = (int)(1); i <= (int)(n); i++) {
    val1[i] = val2[i] = val3[i] = 1;
    for (int j = (int)(S[i - 1] + 1); j <= (int)(S[i]); j++) {
      val1[i] = 1ll * val1[i] * (i - j + 1) % mo;
      val2[i] = 1ll * val2[i] * (i - j) % mo;
      if (j != S[i - 1] + 1) val3[i] = 1ll * val3[i] * (i - j + 1) % mo;
    }
    val4[i] = 1ll * val1[i] * (i - S[i]) % mo;
    V = 1ll * V * val1[i] % mo;
  }
  if (!V) return puts("0"), 0;
  vpr[0] = vsu[n + 1] = 1;
  for (int i = (int)(1); i <= (int)(n); i++)
    vpr[i] = 1ll * vpr[i - 1] * val1[i] % mo;
  for (int i = (int)(n); i >= (int)(1); i--)
    vsu[i] = 1ll * vsu[i + 1] * val1[i] % mo;
  pre[0] = 0;
  suf[n + 1] = n + 1;
  for (int i = (int)(1); i <= (int)(n); i++)
    pre[i] = (val2[i] == 0 ? i : pre[i - 1]);
  for (int i = (int)(n); i >= (int)(1); i--)
    suf[i] = (val2[i] == 0 ? i : suf[i + 1]);
  for (int i = (int)(1); i <= (int)(n); i++)
    if (!val2[i]) val2[i] = 1;
  ifac[0] = 1;
  for (int i = (int)(1); i <= (int)(n); i++)
    ifac[i] = 1ll * ifac[i - 1] * val2[i] % mo;
  iinv[n] = power(ifac[n], mo - 2);
  for (int i = (int)(n); i >= (int)(1); i--)
    iinv[i - 1] = 1ll * iinv[i] * val2[i] % mo;
  for (int i = (int)(1); i <= (int)(n); i++) {
    int vl = 1ll * vpr[a[i] - 1] * val4[a[i]] % mo * iinv[a[i]] % mo;
    int vr = 1ll * vsu[a[i] + 1] * val3[a[i]] % mo * ifac[a[i] - 1] % mo;
    ans = (ans + 1ll * (i - 1 - T1.ask(a[i] - 1)) * V) % mo;
    ans = (ans + 1ll * (i - 1 - T1.ask(a[i])) * V) % mo;
    ans = (ans + mo - 1ll * vl * T2.ask(a[i] + 1, suf[a[i] + 1])) % mo;
    ans = (ans + 1ll * vr * T3.ask(pre[a[i] - 1], a[i] - 1)) % mo;
    T1.change(a[i], 1);
    T2.change(a[i], vr);
    T3.change(a[i], vl);
  }
  ans = 1ll * ans * (mo + 1) / 2 % mo;
  printf("%d\n", ans);
}
