#include<stack>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}
template<unsigned MOD> struct ModInt {
    static const unsigned static_MOD = MOD;
    unsigned x;
    void undef() { x = (unsigned)-1; }
    bool isnan() const { return x == (unsigned)-1; }
    inline int geti() const { return (int)x; }
    ModInt() { x = 0; }
    ModInt(const ModInt &y) { x = y.x; }
    ModInt(int y) { if (y<0 || (int)MOD<=y) y %= (int)MOD; if (y<0) y += MOD; x=y; }
    ModInt(unsigned y) { if (MOD<=y) x = y % MOD; else x = y; }
    ModInt(long long y) { if (y<0 || MOD<=y) y %= MOD; if (y<0) y += MOD; x=y; }
    ModInt(unsigned long long y) { if (MOD<=y) x = y % MOD; else x = y; }
    ModInt &operator+=(const ModInt y) { if ((x += y.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(const ModInt y) { if ((x -= y.x) & (1u<<31)) x += MOD; return *this; }
    ModInt &operator*=(const ModInt y) { x = (unsigned long long)x * y.x % MOD; return *this; }
    ModInt &operator/=(const ModInt y) { x = (unsigned long long)x * y.inv().x % MOD; return *this; }
    ModInt operator-() const { return (x ? MOD-x: 0); }

    ModInt inv() const {
	unsigned a = MOD, b = x; int u = 0, v = 1;
	while (b) {
	    int t = a / b;
	    a -= t * b; swap(a, b);
	    u -= t * v; swap(u, v);
	}
	if (u < 0) u += MOD;
	return ModInt(u);
    }
    ModInt pow(long long y) const {
	ModInt b = *this, r = 1;
	if (y < 0) { b = b.inv(); y = -y; }
	for (; y; y>>=1) {
	    if (y&1) r *= b;
	    b *= b;
	}
	return r;
    }
    friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
    friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
    friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
    friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
    friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
    friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
    friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};

const LL MOD = 1000000007;
typedef ModInt<MOD> Mint;
template<class T> struct Fenwick {
    int n;
    T* d;
    Fenwick() : n(0), d(NULL) {}
    Fenwick(int n_) : n(n_) {
	d = new T[n_]();
    }
    Fenwick(const Fenwick &y) : n(y.n) {
	d = new T[n];
	memcpy(d, y.d, sizeof (T) * n);
    }
    ~Fenwick() {
	delete[] d; d = NULL;
	n = 0;
    }
    friend void swap(Fenwick &x, Fenwick &y) {
	swap(x.n, y.n); swap(x.d, y.d);
    }
    Fenwick& operator=(Fenwick y) {
	swap(*this, y);
	return *this;
    }
    inline void add(int i, const T &x) {
	for (; i<n; i|=i+1) d[i] += x;
    }
    inline T sum(int r) {
	T s = T();
	for (; r; r&=r-1) s += d[r-1];
	return s;
    }
    T sum(int l, int r) {
	return sum(r) - sum(l);
    }
    int lower_bound(const T &x) { // sum(ret) < x <= sum(ret+1);
	if (n == 0) return 0;
	int i = 0; T s = T();
	for (int k=1<<__lg(n); k; k>>=1) {
	    if (i+k <= n && s + d[i+k-1] < x) {
		i += k; s += d[i-1];
	    }
	}
	return i;
    }
    int upper_bound(const T &x) { // sum(ret) <= x < sum(ret+1);
	if (n == 0) return 0;
	int i = 0; T s = T();
	for (int k=1<<__lg(n); k; k>>=1) {
	    if (i+k <= n && !(x < s + d[i+k-1])) {
		i += k; s += d[i-1];
	    }
	}
	return i;
    }
};

int N;
int A[200011];
int C[200011];

Mint prdC[200011];
Mint prdX[200011];
Mint CC(int l, int r) {
    Mint x = 1;
    for (int k=l; k<r; k++) x *= C[k] + k - N + 1;
    return x;
}
Mint XX(int l, int r) {
    Mint x = 1;
    for (int k=l; k<r; k++) x *= C[k] + k - N;
    return x;
}

VI G[100011];

void MAIN() {
    scanf("%d", &N);
    REP (i, N) {
	scanf("%d", A+i);
	A[i]--;
	C[A[i]]++;
	G[A[i]].push_back(i);
    }
    for (int i=N; i--;) C[i] += C[i+1];

    prdC[0] = 1;
    prdX[0] = 1;
    bool zero = false;
    REP (i, N) {
	int x = C[i]+i-N+1;
	if (x <= 0) zero = true;
	prdC[i+1] = prdC[i] * x;
	prdX[i+1] = prdX[i] * (x==1? 1: x-1);
    }

    if (zero) {
	puts("0");
	return;
    }

    Mint ans = 0;
    stack<int> st;
    Fenwick<Mint> F(N);
    auto clr = [&]() {
	while (!st.empty()) {
	    int pos = st.top(); st.pop();
	    Mint t = F.sum(pos, pos+1);
	    F.add(pos, -t);
	}
    };

    REP (i, N) {
	int x = C[i]+i-N+1;
	Mint xc = prdX[i+1] / prdC[i+1];
	if (x == 1) clr();
	EACH (e, G[i]) {
	    ans += F.sum(0, *e) * xc * prdC[N] / 2;
	}
	Mint cx = 1 / xc;
	EACH (e, G[i]) {
	    F.add(*e, cx);
	    st.push(*e);
	}
    }

    clr();

    Fenwick<Mint> Z(N);
    for (int i=N; i--;) {
	int x = C[i]+i-N+1;
	Mint cx = prdC[i+1] / prdX[i+1];
	if (x == 1) clr();
	EACH (e, G[i]) {
	    ans += prdC[N]*Z.sum(0, *e) - F.sum(0, *e) * cx * prdC[N] / 2;
	}
	Mint xc = 1 / cx;
	EACH (e, G[i]) {
	    Z.add(*e, 1);
	    F.add(*e, xc);
	    st.push(*e);
	}
    }

    REP (i, N) {
	Mint m = (int)G[i].size();
	ans += prdC[N] * m * (m-1) / 4;
    }

//    ans *= prdC[N];
//    REP (j, N) REP (i, j) {
//	if (A[i] < A[j]) {
////	    Mint tmp = prdC[N] / prdC[A[j]+1] * prdX[A[j]+1] / prdX[A[i]+1] * prdC[A[i]+1];
//	    Mint tmp = CC(0, A[i]+1) * XX(A[i]+1, A[j]+1) * CC(A[j]+1, N);
//	    ans += tmp / 2;
//	}
//	if (A[i] == A[j]) {
//	    ans += prdC[N] / 2;
//	}
//	if (A[i] > A[j]) {
//	    //Mint tmp = prdC[N] / prdC[A[i]+1] * prdX[A[i]+1] / prdX[A[j]+1] * prdC[A[j]+1];
//	    Mint tmp = CC(0, A[j]+1) * XX(A[j]+1, A[i]+1) * CC(A[i]+1, N);
//	    ans += prdC[N] - tmp / 2;
//	}
//    }
    printf("%d\n", ans.geti());
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}

