#include <bits/stdc++.h>
using namespace std;
long long mo = 1000000007, ha = (mo + 1) / 2;
template <class typ>
struct BIT {
  vector<typ> x;
  BIT(int n) : x(n, 0) {}
  typ sum(int a, int b) {
    if (a > b) return 0;
    if (a == 0) {
      typ s = 0;
      for (int i = b; i >= 0; i = (i & (i + 1)) - 1) {
        s += x[i];
        s %= mo;
      }
      return s;
    } else
      return (sum(0, b) + mo - sum(0, a - 1)) % mo;
  }
  void add(int ind, typ f) {
    for (int i = ind; i < x.size(); i |= i + 1) {
      x[i] += f;
      x[i] %= mo;
    }
  }
};
long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long g = a;
  x = 1;
  y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}
long long invMod(long long a, long long m) {
  if (a == 0LL) return 0;
  long long x, y;
  if (extgcd(a, m, x, y) == 1) return (x + m) % m;
  return 0;
}
int a[200100];
vector<int> A;
long long p[200100], q[200100];
long long pq[200100];
long long qp[200100];
int lo[200100], hi[200100];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    A.push_back(a[i]);
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  int now = 0, it = 0;
  for (int i = n - 1; i >= 0; i--) {
    while (it < n && A[it] == i) {
      now++;
      it++;
    }
    p[i] = now;
    now--;
  }
  for (int i = 0; i < n; i++) {
    if (p[i] == 0L) {
      cout << 0 << endl;
      return 0;
    }
  }
  long long sum = 1;
  for (int i = 0; i < n; i++) {
    sum *= p[i];
    sum %= mo;
    q[i] = p[i] - 1;
  }
  hi[n] = n;
  for (int i = n - 1; i >= 0; i--) {
    hi[i] = hi[i + 1];
    if (q[i] < 1L) hi[i] = i;
  }
  lo[0] = -1;
  for (int i = 0; i < n; i++) {
    if (q[i] < 1L) lo[i] = i;
    lo[i + 1] = lo[i];
  }
  long long out = 0;
  pq[0] = qp[0] = 1;
  for (int i = 0; i < n; i++) {
    pq[i + 1] = pq[i] * p[i];
    if (pq[i] < 1) pq[i + 1] = p[i];
    pq[i + 1] %= mo;
    pq[i + 1] *= invMod(q[i], mo);
    pq[i + 1] %= mo;
    qp[i + 1] = qp[i] * q[i];
    if (qp[i] < 1) qp[i + 1] = q[i];
    qp[i + 1] %= mo;
    qp[i + 1] *= invMod(p[i], mo);
    qp[i + 1] %= mo;
  }
  BIT<long long> bit(n + 10);
  for (int i = n - 1; i >= 0; i--) {
    long long t = bit.sum(a[i], hi[a[i]] - 1) * pq[a[i] + 1];
    if (hi[a[i]] == a[i]) t = bit.sum(a[i], a[i]) * pq[a[i] + 1];
    t %= mo;
    out += t * ha;
    out %= mo;
    bit.add(a[i], sum * qp[a[i] + 1] % mo);
  }
  BIT<long long> bi2(n + 10), bi3(n + 10);
  pq[n] = qp[n] = 1;
  for (int i = n - 1; i >= 0; i--) {
    pq[i] = pq[i + 1] * p[i];
    if (pq[i + 1] < 1) pq[i] = p[i];
    pq[i] %= mo;
    pq[i] *= invMod(q[i], mo);
    pq[i] %= mo;
    qp[i] = qp[i + 1] * q[i];
    if (qp[i + 1] < 1) qp[i] = q[i];
    qp[i] %= mo;
    qp[i] *= invMod(p[i], mo);
    qp[i] %= mo;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (q[a[i]] < 1) continue;
    long long t = bi2.sum(lo[a[i]] + 1, a[i] - 1) * pq[a[i] + 1];
    t %= mo;
    t *= ha;
    t %= mo;
    out += (bi3.sum(0, a[i] - 1) * sum - t);
    out %= mo;
    bi2.add(a[i], sum * qp[a[i] + 1] % mo);
    bi3.add(a[i], 1);
  }
  out %= mo;
  out += mo;
  out %= mo;
  cout << out << endl;
}
