#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

typedef long long cat;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

cat pw(cat a, cat e, cat mod) {
	if(e <= 0) return 1;
	cat x = pw(a, e/2, mod);
	x = x * x % mod;
	if(e%2) x = x * a % mod;
	return x;
}

struct fin {
	vector<cat> T;
	cat mod;
	fin(cat mod) : mod(mod) {}
	fin(int N, cat mod) : T(N+10, 0), mod(mod) {}

	int lastone(int x) {return x&(x^(x-1));}

	void put(int pos, cat val) {
		for(int i = pos+1; i < (int)T.size(); i += lastone(i)) {
			T[i] += val;
			if(T[i] >= mod) T[i] -= mod;
		}
	}

	cat get(int pos) {
		cat ret = 0;
		for(int i = pos+1; i > 0; i -= lastone(i)) {
			ret += T[i];
			if(ret >= mod) ret -= mod;
		}
		return ret;
	}
};

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int N;
	cin >> N;
	vector<int> A(N);
	map<int, int> G;
	G[-1] = 0;
	for(int i = 0; i < N; i++) {
		cin >> A[i];
		G[--A[i]] = 0;
	}

	int g = 0;
	vector<int> group_val;
	ALL_THE(G, it) {
		it->ss = g++;
		group_val.push_back(it->ff);
	}
	if(group_val.back() != N-1) {
		cout << "0\n";
		 return 0;
	}
	g--;
	vector<int> group_size(g), group_id(N), num_pos(g, 0);
	for(int i = 0; i < N; i++) {
		group_id[i] = G[A[i]]-1;
		num_pos[group_id[i]]++;
	}
	for(int i = 0; i < g; i++) group_size[i] = group_val[i+1]-group_val[i];

	for(int i = g-2; i >= 0; i--) num_pos[i] += num_pos[i+1]-group_size[i+1];
	for(int i = 0; i < g; i++) if(num_pos[i] < group_size[i]) {
		cout << "0\n";
		return 0;
	}
	cat cnt_tot = 1, mod = 1000000007;
	for(int i = 0; i < g; i++)
		for(int j = num_pos[i]; j > num_pos[i]-group_size[i]; j--) cnt_tot = cnt_tot * j % mod;

	vector<cat> inv(N+2, 1);
	for(int i = 1; i <= N+1; i++) inv[i] = pw(i, mod-2, mod);
	
	cat ans = 0;
	vector<cat> F(g+1, 1), fzero(g+1, 0), Fi(g+1);
	for(int i = 0; i < g; i++) {
		if(num_pos[i] == group_size[i]) F[i+1] = F[i], fzero[i+1] = fzero[i] + 1;
		else F[i+1] = F[i] * (num_pos[i]-group_size[i]) % mod, fzero[i+1] = fzero[i];
		F[i+1] = F[i+1] * inv[num_pos[i]] % mod;
	}
	vector<int> minfzero(g, 0);
	for(int i = 1; i < g; i++) {
		if(fzero[i+1] == fzero[i]) minfzero[i] = minfzero[i-1];
		else minfzero[i] = i;
	}
	for(int i = 0; i <= g; i++) Fi[i] = pw(F[i], mod-2, mod);
	// left group > right group
	fin Fpairs(N, mod), Fsums(N, mod);
	for(int i = N-1; i >= 0; i--) {
		ans += Fpairs.get(group_id[i]-1);
		Fpairs.put(group_id[i], 1);
	}
	for(int i = N-1; i >= 0; i--) {
		int mfz = minfzero[group_id[i]];
		ans -= (Fsums.get(group_id[i]-1) - Fsums.get(mfz-1)) * inv[2] % mod * F[group_id[i]+1] % mod;
		Fsums.put(group_id[i], Fi[group_id[i]+1]);
	}
	// left group <= right group
	Fsums = fin(N, mod);
	for(int i = 0; i < N; i++) {
		int mfz = minfzero[group_id[i]];
		ans += (Fsums.get(group_id[i]) - Fsums.get(mfz-1)) * inv[2] % mod * F[group_id[i]+1] % mod;
		Fsums.put(group_id[i], Fi[group_id[i]+1]);
	}
	ans = ans % mod * cnt_tot % mod;
	if(ans < 0) ans += mod;
	cout << ans << "\n";
	return 0;
}

// look at my code
// my code is amazing
