#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long i64;
inline int read(int f = 1, int x = 0, char ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 2e5+5, P = 1e9+7, Q = P-(P/2);
i64 inv(int i) { return i == 1?1:P-(P/i)*inv(P%i)%P; }
int n, a[N], c[N], p[N], D[N], E[N], f[N], g[N], ans; i64 S;
void add(int x, int d) { for( ; x <= n; x += x&-x) f[x] = (f[x]+d)%P, ++g[x]; }
int askf(int x) { int ret = 0; for( ; x; x -= x&-x) ret = (ret+f[x])%P; return ret; }
int askg(int x) { int ret = 0; for( ; x; x -= x&-x) ret += g[x]; return ret; }
int main()
{
    n = read(), S = 1; for(int i = 1; i <= n; ++i) a[i] = read(), ++c[a[i]];
    for(int i = n; i; --i) c[i] += c[i+1], S = S*max(c[i]-(n-i), 0)%P;
    if(S == 0) return puts("0"), 0; D[0] = 1;
    for(int i = 1; i <= n; ++i)
    {
        i64 w = (c[i]-1-(n-i))*inv(c[i]-(n-i))%P;
        if(w == 0) p[i] = i, D[i] = D[i-1]; else p[i] = p[i-1], D[i] = D[i-1]*w%P;
        E[i] = inv(D[i]);
    }
    for(int i = 1; i <= n; ++i)
    {
        ans = (ans+(askf(a[i])-askf(p[a[i]]-1)+P)%P*S%P*D[a[i]]%P*Q%P)%P;
        add(a[i], E[a[i]]);
    }
    for(int i = 1; i <= n; ++i) f[i] = g[i] = 0;
    for(int i = n; i; --i)
    {
        ans = (ans-(askf(a[i]-1)-askf(p[a[i]]-1)+P)%P*S%P*D[a[i]]%P*Q%P+P)%P;
        ans = (ans+askg(a[i]-1)*S%P)%P;
        add(a[i], E[a[i]]);
    }    
    printf("%d\n", ans);
    return 0;
}