#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
const long long md = 1e9 + 7;
int n;
int a[maxn];
vector<int> vec[maxn];
long long tr1[maxn << 2], tr2[maxn << 2];
int cnt[maxn << 2], tg[maxn << 2];
long long f[maxn], mtg[maxn << 2];
void Add_to(int h, int v) {
  tg[h] += v;
  tr2[h] = (tr2[h] + tr1[h] * v) % md;
}
void Mult(int h, int v) {
  mtg[h] = mtg[h] * v % md;
  tr1[h] = tr1[h] * v % md;
  tr2[h] = tr2[h] * v % md;
}
void push_down(int h) {
  if (tg[h]) {
    Add_to(h << 1, tg[h]);
    Add_to(h << 1 | 1, tg[h]);
    tg[h] = 0;
  }
  if (mtg[h] != 1) {
    Mult(h << 1, mtg[h]);
    Mult(h << 1 | 1, mtg[h]);
    mtg[h] = 1;
  }
}
void push_up(int h) {
  cnt[h] = cnt[h << 1] + cnt[h << 1 | 1];
  tr1[h] = tr1[h << 1] + tr1[h << 1 | 1];
  tr2[h] = tr2[h << 1] + tr2[h << 1 | 1];
}
void Add_to(int h, int l, int r, int s, int t) {
  if (s <= l && r <= t) {
    Add_to(h, 1);
    return;
  }
  push_down(h);
  if (t <= ((l + r) >> 1))
    Add_to(h << 1, l, ((l + r) >> 1), s, t);
  else if (s > ((l + r) >> 1))
    Add_to(h << 1 | 1, ((l + r) >> 1) + 1, r, s, t);
  else {
    Add_to(h << 1, l, ((l + r) >> 1), s, ((l + r) >> 1));
    Add_to(h << 1 | 1, ((l + r) >> 1) + 1, r, s, t);
  }
  push_up(h);
}
void Insert(int h, int l, int r, int p, long long s1, long long s2) {
  if (l == r) {
    tr1[h] = s1, tr2[h] = s2;
    cnt[h] = 1;
    return;
  }
  push_down(h);
  if (p <= ((l + r) >> 1))
    Insert(h << 1, l, ((l + r) >> 1), p, s1, s2);
  else
    Insert(h << 1 | 1, ((l + r) >> 1) + 1, r, p, s1, s2);
  push_up(h);
}
int Query_cnt(int h, int l, int r, int s, int t) {
  if (s <= l && r <= t) return cnt[h];
  push_down(h);
  if (t <= ((l + r) >> 1))
    return Query_cnt(h << 1, l, ((l + r) >> 1), s, t);
  else if (s > ((l + r) >> 1))
    return Query_cnt(h << 1 | 1, ((l + r) >> 1) + 1, r, s, t);
  else
    return Query_cnt(h << 1, l, ((l + r) >> 1), s, ((l + r) >> 1)) +
           Query_cnt(h << 1 | 1, ((l + r) >> 1) + 1, r, ((l + r) >> 1) + 1, t);
}
long long Query1(int h, int l, int r, int s, int t) {
  if (s <= l && r <= t) return tr1[h];
  push_down(h);
  if (t <= ((l + r) >> 1))
    return Query1(h << 1, l, ((l + r) >> 1), s, t);
  else if (s > ((l + r) >> 1))
    return Query1(h << 1 | 1, ((l + r) >> 1) + 1, r, s, t);
  else
    return Query1(h << 1, l, ((l + r) >> 1), s, ((l + r) >> 1)) +
           Query1(h << 1 | 1, ((l + r) >> 1) + 1, r, ((l + r) >> 1) + 1, t);
}
long long Query2(int h, int l, int r, int s, int t) {
  if (s <= l && r <= t) return tr2[h];
  push_down(h);
  if (t <= ((l + r) >> 1))
    return Query2(h << 1, l, ((l + r) >> 1), s, t);
  else if (s > ((l + r) >> 1))
    return Query2(h << 1 | 1, ((l + r) >> 1) + 1, r, s, t);
  else
    return Query2(h << 1, l, ((l + r) >> 1), s, ((l + r) >> 1)) +
           Query2(h << 1 | 1, ((l + r) >> 1) + 1, r, ((l + r) >> 1) + 1, t);
}
void Build_tree(int h, int l, int r) {
  mtg[h] = 1;
  if (l == r) return;
  Build_tree(h << 1, l, ((l + r) >> 1));
  Build_tree(h << 1 | 1, ((l + r) >> 1) + 1, r);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    vec[a[i]].push_back(i);
  }
  Build_tree(1, 1, n);
  long long sum0 = 1, sum2 = 0;
  int siz = 0;
  for (int i = n; i >= 1; i--) {
    sum2 = (sum2 + vec[i].size() * (long long)(vec[i].size() - 1) / 2 % md *
                       sum0 % md) %
           md;
    sum2 = (sum2 + vec[i].size() * Query1(1, 1, n, 1, n)) % md;
    for (vector<int>::iterator it = vec[i].begin(); it != vec[i].end(); it++)
      Add_to(1, 1, n, *it, n);
    for (vector<int>::iterator it = vec[i].begin(); it != vec[i].end(); it++)
      Insert(1, 1, n, *it, sum0, sum0 * Query_cnt(1, 1, n, 1, *it) % md);
    siz += vec[i].size();
    f[i] =
        (Query2(1, 1, n, 1, n) - sum2 + md + f[i + 1] * (siz - (n - i))) % md;
    if (siz - (n - i) - 2 < 0)
      sum2 = 0;
    else
      sum2 = sum2 * (siz - (n - i) - 2) % md;
    sum0 = sum0 * (siz - (n - i)) % md;
    Mult(1, siz - (n - i) - 1);
  }
  printf("%lld\n", f[1]);
  return 0;
}
