#include <bits/stdc++.h>
using namespace std;
const long long q = 1000000007;
long long n, a[200010], p, r = 1, u = 1;
vector<long long> x[200010];
pair<long long, long long> f[200010];
inline void operator+=(pair<long long, long long> &a,
                       pair<long long, long long> b) {
  a.first += b.first;
  a.second = (a.second + b.second) % q;
}
inline long long power(long long a, long long b) {
  if (!b) return 1;
  long long c = power(a, b >> 1);
  c = (long long)c * c % q;
  if (b & 1) c = (long long)c * a % q;
  return c;
}
inline pair<long long, long long> sum(long long i) {
  pair<long long, long long> k = {};
  for (; i; i ^= i & -i) k += f[i];
  return k;
}
inline void add(long long i, pair<long long, long long> k) {
  for (; i <= n; i += i & -i) f[i] += k;
}
signed main() {
  long long i, j, k;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    x[a[i]].push_back(i);
  }
  for (i = n, k = 1; i > 0; i--) {
    k--;
    j = x[i].size();
    p = (p + ((long long)j * (j - 1) / 2 + (long long)j * k) % q * 500000004) %
        q;
    k += j;
    for (auto l : x[i]) {
      pair<long long, long long> w = sum(l - 1);
      p = (p + w.first - (long long)w.second * u) % q;
    }
    for (auto l : x[i]) add(l, make_pair(1, power(u, q - 2)));
    u = (long long)u * (1 - power(k, q - 2)) % q;
    r = (long long)r * k % q;
  }
  p = (long long)p * r % q;
  p = (p + q) % q;
  printf("%lld\n", p);
  return 0;
}
