#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long maxn = 200005;
long long t[maxn];
long long t2[maxn];
long long inv[maxn];
void add(long long t[], long long i, long long value) {
  for (; i < maxn; i += (i + 1) & -(i + 1)) t[i] += value;
}
long long sum(long long t[], long long i) {
  long long res = 0;
  for (; i >= 0; i -= (i + 1) & -(i + 1)) res += t[i];
  return res;
}
long long exp(long long a, long long b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    long long y = exp(a, b / 2);
    return (y * y) % MOD;
  }
  return (a * exp(a, b - 1)) % MOD;
}
int main() {
  long long N;
  cin >> N;
  long long A[N];
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }
  long long freq[N + 1];
  for (long long i = 0; i < N + 1; i++) {
    freq[i] = 0;
  }
  for (long long i = 0; i < N; i++) {
    freq[A[i]]++;
  }
  for (long long i = N - 1; i >= 0; i--) {
    freq[i] = freq[i + 1] + freq[i] - 1;
  }
  for (long long i = 1; i < N + 1; i++) {
    if (freq[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  long long partial[N + 1];
  long long check[N + 1];
  long long pcheck[N + 1];
  for (long long i = 0; i < N + 1; i++) check[i] = 0;
  partial[0] = 1;
  for (long long i = 1; i < N + 1; i++) {
    partial[i] = (partial[i - 1] * (freq[i] - 1)) % MOD;
    partial[i] = (partial[i] * exp(freq[i], MOD - 2)) % MOD;
    if (partial[i] == 0) {
      partial[i] = 1;
      check[i] = 1;
    }
  }
  pcheck[0] = 0;
  long long u = 0;
  for (long long i = 1; i < N + 1; i++) {
    if (check[i] == 1) {
      for (long long j = u; j < i; j++) {
        pcheck[j] = i;
      }
      u = i;
    }
  }
  for (long long j = u; j < N + 1; j++) {
    pcheck[j] = N + 1;
  }
  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    long long carry = sum(t, A[i]);
    ans = (ans + partial[A[i]] * carry) % MOD;
    add(t, A[i], exp(partial[A[i]], MOD - 2));
    add(t, pcheck[A[i]], -exp(partial[A[i]], MOD - 2));
  }
  for (long long i = 0; i < N / 2; i++) {
    long long b = A[N - 1 - i];
    A[N - 1 - i] = A[i];
    A[i] = b;
  }
  for (long long i = 0; i < N; i++) {
    long long carry = sum(t2, A[i]);
    long long counter = sum(inv, A[i] - 1);
    ans = (ans + 2 * counter - partial[A[i]] * carry) % MOD;
    add(t2, A[i] + 1, exp(partial[A[i]], MOD - 2));
    add(t2, pcheck[A[i]], -exp(partial[A[i]], MOD - 2));
    add(inv, A[i], 1);
  }
  ans = (ans * exp(2, MOD - 2)) % MOD;
  for (long long i = 1; i < N + 1; i++) {
    ans = (ans * freq[i]) % MOD;
  }
  while (ans < 0) ans += MOD;
  cout << ans << endl;
  return 0;
}
