#include <bits/stdc++.h>
inline int fuck(int x, int p) {
  register int y = 1;
  for (; p; p >>= 1) {
    if (p & 1) y = 1LL * y * x % 1000000007;
    x = 1LL * x * x % 1000000007;
  }
  return y;
}
const int inv2 = fuck(2, 1000000007 - 2);
int n, a[100010], b[100010], c[100010], d[100010], BIT[100010], all, ans;
inline void up(int &x, int y) {
  if ((x += y) >= 1000000007) x -= 1000000007;
}
inline int lowbit(int x) { return x & -x; }
inline void add(int x, int y) {
  for (; x <= n; x += lowbit(x)) up(BIT[x], y);
}
inline int qry(int x) {
  register int y = 0;
  for (; x; x -= lowbit(x)) up(y, BIT[x]);
  return y;
}
int count[100010];
inline void add(int x) {
  for (; x <= n; x += lowbit(x)) count[x]++;
}
inline int get(int x) {
  register int y = 0;
  for (; x; x -= lowbit(x)) y += count[x];
  return y;
}
int l[100010], r[100010];
int main() {
  for (int i = scanf("%d", &n); i <= n; i++) scanf("%d", &a[i]), b[a[i]]++;
  for (int i = n; i >= 1; i--) b[i] += b[i + 1];
  all = 1;
  for (int i = 1; i <= n; i++) {
    if ((b[i] -= n - i) <= 0) return puts("0"), 0;
    all = 1LL * all * b[i] % 1000000007;
  }
  for (int i = 1; i <= n; i++)
    c[i] = 1LL * (b[i] - 1) * fuck(b[i], 1000000007 - 2) % 1000000007;
  l[0] = 0;
  for (int i = 1; i <= n; i++) l[i] = c[i] ? l[i - 1] : i;
  r[n + 1] = n + 1;
  for (int i = n; i >= 1; i--) r[i] = c[i] ? r[i + 1] : i;
  d[0] = 1;
  for (int i = 1; i <= n; i++)
    d[i] = d[i - 1] ? 1LL * d[i - 1] * c[i] % 1000000007 : c[i];
  for (int i = n; i >= 1; i--) {
    int tmp = (qry(r[a[i]] - 1) - qry(a[i] - 1) + 1000000007) % 1000000007;
    tmp = 1LL * tmp * all % 1000000007;
    tmp = 1LL * tmp * inv2 % 1000000007;
    tmp = 1LL * tmp * fuck(d[a[i]], 1000000007 - 2) % 1000000007;
    up(ans, tmp);
    add(a[i], d[a[i]]);
  }
  memset(BIT, 0, sizeof(BIT));
  for (int i = 1; i <= n; i++) {
    int tmp = (qry(r[a[i]] - 1) - qry(a[i]) + 1000000007) % 1000000007;
    tmp = 1LL * tmp * all % 1000000007;
    tmp = 1LL * tmp * inv2 % 1000000007;
    tmp = 1LL * tmp * fuck(d[a[i]], 1000000007 - 2) % 1000000007;
    tmp = (1LL * (get(n) - get(a[i])) * all % 1000000007 - tmp + 1000000007) %
          1000000007;
    up(ans, tmp);
    add(a[i], d[a[i]]);
    add(a[i]);
  }
  printf("%d\n", ans);
  return 0;
}
