#include <bits/stdc++.h>
using namespace std;
constexpr int MAX_N = 2E5, MOD = 1E9 + 7, INV2 = (MOD + 1) / 2;
void inc(int &a, int b) {(a += b) >= MOD ? a -= MOD : 0;}
int n;
int a[MAX_N], b[MAX_N + 1], pre[MAX_N + 1], suf[MAX_N + 1];
struct Node {
    int s, p, c;
    Node() {}
    Node(int s, int p, int c) : s(s), p(p), c(c) {}
} t[4 * MAX_N];
Node operator+(const Node &lhs, const Node &rhs) {return Node((1LL * lhs.s * rhs.p + rhs.s) % MOD, 1LL * lhs.p * rhs.p % MOD, lhs.c + rhs.c);}
void build(int v, int l, int r) {
    if (r - l == 1) {
        t[v] = Node(0, b[l] - 1, 0);
        return;
    }
    int mid = (l + r) / 2;
    build(2 * v, l, mid);
    build(2 * v + 1, mid, r);
    t[v] = t[2 * v] + t[2 * v + 1];
}
Node rangeSum(int v, int l, int r, int x, int y) {
    if (l >= y || r <= x)
        return Node(0, 1, 0);
    if (l >= x && r <= y)
        return t[v];
    int mid = (l + r) / 2;
    Node lc = rangeSum(2 * v, l, mid, x, y);
    Node rc = rangeSum(2 * v + 1, mid, r, x, y);
    return lc + rc;
}
void modify(int v, int l, int r, int x) {
    if (r - l == 1) {
        inc(t[v].s, pre[x + 1]);
        ++t[v].c;
        return;
    }
    int mid = (l + r) / 2;
    if (x < mid)
        modify(2 * v, l, mid, x);
    else
        modify(2 * v + 1, mid, r, x);
    t[v] = t[2 * v] + t[2 * v + 1];
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        ++b[a[i]];
    }
    for (int i = n - 1; i >= 0; --i)
        b[i] += b[i + 1];
    for (int i = 0; i < n; ++i)
        b[i] = b[i] - n + i + 1;
    pre[0] = 1;
    for (int i = 0; i < n; ++i)
        pre[i + 1] = 1LL * pre[i] * b[i] % MOD;
    suf[n] = 1;
    for (int i = n - 1; i >= 0; --i)
        suf[i] = 1LL * suf[i + 1] * b[i] % MOD;
    int ans = 0;
    build(1, 0, n);
    for (int i = 0; i < n; ++i) {
        ans = (ans + 1LL * rangeSum(1, 0, n, 0, a[i] + 1).s * suf[a[i] + 1] % MOD * INV2) % MOD;
        modify(1, 0, n, a[i]);
    }
    build(1, 0, n);
    for (int i = n - 1; i >= 0; --i) {
        Node x = rangeSum(1, 0, n, 0, a[i]);
        ans = (ans + 1LL * pre[n] * x.c - 1LL * x.s * (b[a[i]] - 1) % MOD * suf[a[i] + 1] % MOD * INV2) % MOD;
        modify(1, 0, n, a[i]);
    }
    cout << (ans + MOD) % MOD << "\n";
    return 0;
}