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