#include <algorithm>
#include <cstdio>

const int N = 200000;
const int MOD = 1e9 + 7;

void update(int& x, int a)
{
    x += a;
    if (x >= MOD) {
        x -= MOD;
    }
}

int inv(int a)
{
    return a == 1 ? 1 : 1LL * (MOD - MOD / a) * inv(MOD % a) % MOD;
}

int n, a[N], id[N];

bool by_a(int i, int j)
{
    return a[i] < a[j];
}

struct DS
{
    void multiply(int a)
    {
        if (a) {
            sum = 1LL * sum * a % MOD;
            multiple = 1LL * multiple * a % MOD;
            inv_multiple = 1LL * inv_multiple * inv(a) % MOD;
        } else {
            now ++;
            sum = 0;
            multiple = inv_multiple = 1;
        }
    }

    void add(int k, int v)
    {
        update(sum, v);
        v = 1LL * v * inv_multiple % MOD;
        for (; k < n; k += ~k & k + 1) {
            update(at(k), v);
        }
    }

    int ask(int k)
    {
        int result = 0;
        for (; ~k; k -= ~k & k + 1) {
            update(result, at(k));
        }
        return 1LL * multiple * result % MOD;
    }

    int& at(int i)
    {
        if (ts[i] != now) {
            ts[i] = now;
            mem[i] = 0;
        }
        return mem[i];
    }

    int sum = 0, multiple = 1, inv_multiple = 1, now, ts[N], mem[N];
} ds;

int main()
{
#ifdef LOCAL_JUDGE
    freopen("E.in", "r", stdin);
#endif
    scanf("%d", &n);
    for (int i = 0; i < n; ++ i) {
        scanf("%d", a + i);
        id[i] = i;
    }
    std::sort(id, id + n, by_a);
    // for i < j
    // if id[i] < id[j], count # of perms where p[i] > p[j]
    // if id[i] > id[j], count # of perms where p[i] < p[j] = (# of perms) - (# of perms where p[i] > p[j])
    int suffix = 1;
    int result = 0;
    for (int i = n - 1; i >= 0; -- i) {
        int aa = a[id[i]];
        result = 1LL * result * (aa + MOD - i) % MOD;
        int tmp = ds.ask(id[i]);
        tmp = (1LL * ds.sum + MOD - tmp + MOD - tmp) % MOD;
        update(result, 1LL * tmp * (aa + MOD - i) % MOD * (aa + MOD - i - 1) % MOD);
        ds.multiply((aa + MOD - i - 1) % MOD);
        ds.add(id[i], suffix);
        suffix = 1LL * suffix * (aa + MOD - i) % MOD;
    }
    ds.now ++;
    int inversion = 0;
    for (int i = n - 1; i >= 0; -- i) {
        update(inversion, ds.ask(id[i]));
        ds.add(id[i], 1);
    }
    update(result, 2LL * inversion * suffix % MOD);
    printf("%d\n", static_cast<int>(1LL * result * inv(2) % MOD));
}
