#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long n, inv2 = (1000000007 + 1) / 2;
long long a[N], c[N], s[N], lst[N], t[N];
long long ksm(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % 1000000007;
    a = a * a % 1000000007, b >>= 1;
  }
  return res;
}
long long add(long long x, long long y) {
  for (; x <= n; x += x & (-x)) t[x] += y;
}
long long qry(long long x) {
  long long res = 0;
  for (; x; x -= x & (-x)) res = (res + t[x]) % 1000000007;
  return res;
}
int main() {
  long long sum = 1, ans = 0, res = 0;
  scanf("%lld", &n);
  for (int i = (1); i <= (n); ++i) scanf("%lld", &a[i]), c[a[i]]++;
  for (int i = (n); i >= (1); --i) c[i] += c[i + 1];
  for (int i = (1); i <= (n); ++i)
    c[i] -= (n - i), sum = sum * c[i] % 1000000007;
  if (sum <= 0) {
    puts("0");
    return 0;
  }
  for (int i = (1); i <= (n); ++i) {
    if (c[i] > 1) {
      lst[i] = lst[i - 1];
      s[i] = (c[i] - 1) * ksm(c[i], 1000000007 - 2) % 1000000007;
      if (s[i - 1] > 0) s[i] = s[i] * s[i - 1] % 1000000007;
    } else
      lst[i] = i;
  }
  for (int i = (1); i <= (n); ++i) {
    ans = (ans + (qry(a[i]) - qry(lst[a[i]]) + 1000000007) % 1000000007 *
                     s[a[i]] % 1000000007 * sum % 1000000007) %
          1000000007;
    add(a[i], ksm(s[a[i]], 1000000007 - 2));
  }
  ans = ans * inv2 % 1000000007;
  memset(t, 0, sizeof(t));
  for (int i = (1); i <= (n); ++i) {
    ans = (ans + (i - 1 - qry(a[i])) * sum % 1000000007) % 1000000007;
    add(a[i], 1);
  }
  memset(t, 0, sizeof(t));
  for (int i = (n); i >= (1); --i)
    if (s[a[i]]) {
      res = (res + (qry(a[i]) - qry(lst[a[i]]) + 1000000007) % 1000000007 *
                       s[a[i]] % 1000000007 * sum % 1000000007) %
            1000000007;
      add(a[i], ksm(s[a[i]], 1000000007 - 2));
    }
  res = res * inv2 % 1000000007;
  printf("%lld\n", (ans - res) % 1000000007);
  return 0;
}
