// Shaazzz
#include<bits/stdc++.h>
using namespace std;
const int N = 1000006, Mod = 1e9 + 7;
int n, tot, F[N], dp[N];
inline void Add(int i)
{
for (; i < N; i += i & -i)
F[i] ++;
}
inline int Get(int i)
{
int ret = 0;
for (; i; i -= i & -i)
ret += F[i];
return (ret);
}
int main()
{
scanf("%d", &n);
dp[1] = 0;
for (int i = 2; i < N; i++)
dp[i] = (1ll * dp[i - 1] * i + 1ll * (i - 1) * (i + 1) / 2) % Mod;
for (int i = 1; i <= n; i++)
{
int a, c;
scanf("%d", &a);
c = a - 1 - Get(a);
Add(a);
tot = (tot + 1ll * c * (dp[n - i] + (n - i + 2) / 2)) % Mod;
}
return !printf("%d", tot);
}
Ly8gU2hhYXp6egojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgTiA9IDEwMDAwMDYsIE1vZCA9IDFlOSArIDc7CmludCBuLCB0b3QsIEZbTl0sIGRwW05dOwppbmxpbmUgdm9pZCBBZGQoaW50IGkpCnsKICAgIGZvciAoOyBpIDwgTjsgaSArPSBpICYgLWkpCiAgICAgICAgRltpXSArKzsKfQppbmxpbmUgaW50IEdldChpbnQgaSkKewogICAgaW50IHJldCA9IDA7CiAgICBmb3IgKDsgaTsgaSAtPSBpICYgLWkpCiAgICAgICAgcmV0ICs9IEZbaV07CiAgICByZXR1cm4gKHJldCk7Cn0KaW50IG1haW4oKQp7CiAgICBzY2FuZigiJWQiLCAmbik7CiAgICBkcFsxXSA9IDA7CiAgICBmb3IgKGludCBpID0gMjsgaSA8IE47IGkrKykKICAgICAgICBkcFtpXSA9ICgxbGwgKiBkcFtpIC0gMV0gKiBpICsgMWxsICogKGkgLSAxKSAqIChpICsgMSkgLyAyKSAlIE1vZDsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykKICAgIHsKICAgICAgICBpbnQgYSwgYzsKICAgICAgICBzY2FuZigiJWQiLCAmYSk7CiAgICAgICAgYyA9IGEgLSAxIC0gR2V0KGEpOwogICAgICAgIEFkZChhKTsKICAgICAgICB0b3QgPSAodG90ICsgMWxsICogYyAqIChkcFtuIC0gaV0gKyAobiAtIGkgKyAyKSAvIDIpKSAlIE1vZDsKICAgIH0KICAgIHJldHVybiAhcHJpbnRmKCIlZCIsIHRvdCk7Cn0K