#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(long long a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10) write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(long long a) {
  write(a);
  puts("");
}
inline void wri(long long a) {
  write(a);
  putchar(' ');
}
inline unsigned long long rnd() {
  return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4;
}
const int N = 200005, mod = 1e9 + 7;
int to[N], s[N], a[N], jb[N];
long long g[N], f[N], c[N], myh, sum = 1;
long long ksm(long long a, int b) {
  int ans = 1;
  for (; b; b >>= 1) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
  }
  return ans;
}
long long ask(int pos) {
  long long ans = 0;
  for (int i = pos; i; i -= i & -i) ans += c[i];
  return ans;
}
void change(int pos, int de) {
  for (int i = pos; i < N; i += i & -i) c[i] += de;
}
int main() {
  int n = read();
  for (int i = (int)(1); i <= (int)(n); i++) to[a[i] = read()]++;
  for (int i = (int)(n); i >= (int)(1); i--) {
    to[i] += to[i + 1];
    sum = sum * (s[i] = to[i] - (n - i)) % mod;
  }
  for (int i = (int)(f[0] = g[0] = 1); i <= (int)(n); i++) {
    f[i] = f[i - 1];
    jb[i] = jb[i - 1];
    if (s[i] > 1)
      f[i] = f[i] * (s[i] - 1) % mod * ksm(s[i], mod - 2) % mod;
    else
      jb[i] = i - 1;
    g[i] = ksm(f[i], mod - 2);
  }
  for (int i = (int)(1); i <= (int)(n); i++) {
    change(a[i], 1);
    myh += i - ask(a[i]);
  }
  memset((c), 0, sizeof(c));
  long long ans = myh * 2 % mod;
  for (int i = (int)(1); i <= (int)(n); i++) {
    ans = (ans + (ask(a[i]) - ask(jb[a[i]])) * f[a[i]]) % mod;
    change(a[i], g[a[i]]);
  }
  memset((c), 0, sizeof(c));
  for (int i = (int)(n); i >= (int)(1); i--) {
    ans = (ans - (ask(a[i] - 1) - ask(jb[a[i]])) * f[a[i]]) % mod;
    change(a[i], g[a[i]]);
  }
  ans = (ans * (mod + 1) / 2 % mod + mod) % mod;
  cout << ans * sum % mod << endl;
}
