#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 200005;
const int mod = 1e9 + 7;

struct mint {
	int val;
	mint() { val = 0; }
	mint(const lint& v) {
		val = (-mod <= v && v < mod) ? v : v % mod;
		if (val < 0) val += mod;
	}

	friend ostream& operator<<(ostream& os, const mint& a) { return os << a.val; }
	friend bool operator==(const mint& a, const mint& b) { return a.val == b.val; }
	friend bool operator!=(const mint& a, const mint& b) { return !(a == b); }
	friend bool operator<(const mint& a, const mint& b) { return a.val < b.val; }

	mint operator-() const { return mint(-val); }
	mint& operator+=(const mint& m) { if ((val += m.val) >= mod) val -= mod; return *this; }
	mint& operator-=(const mint& m) { if ((val -= m.val) < 0) val += mod; return *this; }
	mint& operator*=(const mint& m) { val = (lint)val*m.val%mod; return *this; }
	friend mint ipow(mint a, lint p) {
		mint ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
		return ans;
	}
	friend mint inv(const mint& a) { assert(a.val); return ipow(a, mod - 2); }
	mint& operator/=(const mint& m) { return (*this) *= inv(m); }

	friend mint operator+(mint a, const mint& b) { return a += b; }
	friend mint operator-(mint a, const mint& b) { return a -= b; }
	friend mint operator*(mint a, const mint& b) { return a *= b; }
	friend mint operator/(mint a, const mint& b) { return a /= b; }
	operator int64_t() const {return val; }
};

int n, a[MAXN];
int val[MAXN];

mint f(int x, int y){
	int s, e;
	if(x == y){
		s = e = -1;
	}
	else{
		s = min(a[x], a[y]);
		e = max(a[x], a[y]);
	}
	mint ret = 1;
	for(int i=1; i<=n; i++){
		ret *= val[i] - (s < i && i <= e);
	}
	ret *= mint((mod + 1) / 2);
	return ret;
}

struct bit{
	int tree[MAXN];
	void add(int x, int v){
		for(int i=x; i<MAXN; i+=i&-i) tree[i] += v;
	}
	int query(int x){
		int ret = 0;
		for(int i=x; i; i-=i&-i) ret += tree[i];
		return ret;
	}
}bit;

vector<int> v[MAXN];
mint pref[MAXN], suff[MAXN];
mint foo[MAXN], bar[MAXN];

mint dnc(int l, int r){
	if(l == r) return mint(0);
	int m = (l+r)/2;
	mint ret = dnc(l, m) + dnc(m+1, r);
	vector<pi> L, R;
	for(int i=l; i<=m; i++){
		for(auto &j : v[i]){
			L.emplace_back(j, i);
		}
	}
	for(int i=m+1; i<=r; i++){
		for(auto &j : v[i]){
			R.emplace_back(j, i);
		}
	}
	foo[m + 1] = pref[m];
	for(int i=m; i>=l; i--){
		foo[i] = foo[i + 1] * mint(val[i] - 1) / mint(val[i]);
	}
	bar[m] = suff[m+1];
	for(int i=m+1; i<=r; i++){
		bar[i] = bar[i - 1] * mint(val[i] - 1) / mint(val[i]);
	}
	sort(all(L));
	sort(all(R));
	int ptr = 0;
	mint baz = 0;
	for(int i=0; i<sz(R); i++){
		while(ptr < sz(L) && L[ptr].first <= R[i].first){
			baz += foo[L[ptr].second + 1];
			ptr++;
		}
		ret += baz * bar[R[i].second];
	}
	return ret;
}

bool init(){
	memset(val, 0, sizeof(val));
	for(int i=1; i<=n; i++) val[i] = 1;
	for(int i=0; i<n; i++) val[a[i]+1]--;
	for(int i=1; i<=n; i++){
		val[i] += val[i-1];
		if(val[i] <= 0) return 1;
	}
	pref[0] = 1;
	for(int i=1; i<=n; i++){
		pref[i] = pref[i-1] * mint(val[i]);
	}
	suff[n+1] = 1;
	for(int i=n; i; i--){
		suff[i] = suff[i+1] * mint(val[i]);
	}
	return 0;
}

mint solve(){
	if(init()) return 0;
	for(int i=1; i<=n; i++) v[i].clear();
	for(int i=0; i<n; i++){
		v[a[i]].push_back(i);
	}
	return dnc(1, n) / mint(2);
	/*
	for(int i=0; i<n; i++){
		for(int j=0; j<i; j++){
			if(a[j] < a[i]){
				ret += f(j, i);
			}
		}
	}
	*/
}

int main(){
	scanf("%d",&n);
	for(int i=0; i<n; i++) scanf("%d",&a[i]);
	if(init()){
		puts("0");
		return 0;
	}
	mint ret = 0;
	{
		mint inv = 0;
		for(int i=n-1; i>=0; i--){
			inv += mint(bit.query(a[i]) + bit.query(a[i] - 1));
			bit.add(a[i], 1);
		}
		ret += inv * f(-69, -69);
	}
	ret += solve();
	reverse(a, a + n);
	ret -= solve();
	cout << ret << endl;
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

Snuke has an integer sequence A whose length is N. He likes permutations of (1, 2, ..., N), P, that satisfy the following condition:

* P_i \leq A_i for all i ( 1 \leq i \leq N ).



Snuke is interested in the inversion numbers of such permutations. Find the sum of the inversion numbers over all permutations that satisfy the condition. Since this can be extremely large, compute the sum modulo 10^9+7.

Constraints

* 1 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N ( 1 \leq i \leq N )
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
A_1 A_2 ... A_N


Output

Print the sum of the inversion numbers over all permutations that satisfy the condition.

Examples

Input

3
2 3 3


Output

4


Input

6
4 2 5 1 6 3


Output

7


Input

5
4 4 4 4 4


Output

0


Input

30
22 30 15 20 10 29 11 29 28 11 26 10 18 28 22 5 29 16 24 24 27 10 21 30 29 19 28 27 18 23


Output

848414012

### Response

```cpp
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 200005;
const int mod = 1e9 + 7;

struct mint {
	int val;
	mint() { val = 0; }
	mint(const lint& v) {
		val = (-mod <= v && v < mod) ? v : v % mod;
		if (val < 0) val += mod;
	}

	friend ostream& operator<<(ostream& os, const mint& a) { return os << a.val; }
	friend bool operator==(const mint& a, const mint& b) { return a.val == b.val; }
	friend bool operator!=(const mint& a, const mint& b) { return !(a == b); }
	friend bool operator<(const mint& a, const mint& b) { return a.val < b.val; }

	mint operator-() const { return mint(-val); }
	mint& operator+=(const mint& m) { if ((val += m.val) >= mod) val -= mod; return *this; }
	mint& operator-=(const mint& m) { if ((val -= m.val) < 0) val += mod; return *this; }
	mint& operator*=(const mint& m) { val = (lint)val*m.val%mod; return *this; }
	friend mint ipow(mint a, lint p) {
		mint ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
		return ans;
	}
	friend mint inv(const mint& a) { assert(a.val); return ipow(a, mod - 2); }
	mint& operator/=(const mint& m) { return (*this) *= inv(m); }

	friend mint operator+(mint a, const mint& b) { return a += b; }
	friend mint operator-(mint a, const mint& b) { return a -= b; }
	friend mint operator*(mint a, const mint& b) { return a *= b; }
	friend mint operator/(mint a, const mint& b) { return a /= b; }
	operator int64_t() const {return val; }
};

int n, a[MAXN];
int val[MAXN];

mint f(int x, int y){
	int s, e;
	if(x == y){
		s = e = -1;
	}
	else{
		s = min(a[x], a[y]);
		e = max(a[x], a[y]);
	}
	mint ret = 1;
	for(int i=1; i<=n; i++){
		ret *= val[i] - (s < i && i <= e);
	}
	ret *= mint((mod + 1) / 2);
	return ret;
}

struct bit{
	int tree[MAXN];
	void add(int x, int v){
		for(int i=x; i<MAXN; i+=i&-i) tree[i] += v;
	}
	int query(int x){
		int ret = 0;
		for(int i=x; i; i-=i&-i) ret += tree[i];
		return ret;
	}
}bit;

vector<int> v[MAXN];
mint pref[MAXN], suff[MAXN];
mint foo[MAXN], bar[MAXN];

mint dnc(int l, int r){
	if(l == r) return mint(0);
	int m = (l+r)/2;
	mint ret = dnc(l, m) + dnc(m+1, r);
	vector<pi> L, R;
	for(int i=l; i<=m; i++){
		for(auto &j : v[i]){
			L.emplace_back(j, i);
		}
	}
	for(int i=m+1; i<=r; i++){
		for(auto &j : v[i]){
			R.emplace_back(j, i);
		}
	}
	foo[m + 1] = pref[m];
	for(int i=m; i>=l; i--){
		foo[i] = foo[i + 1] * mint(val[i] - 1) / mint(val[i]);
	}
	bar[m] = suff[m+1];
	for(int i=m+1; i<=r; i++){
		bar[i] = bar[i - 1] * mint(val[i] - 1) / mint(val[i]);
	}
	sort(all(L));
	sort(all(R));
	int ptr = 0;
	mint baz = 0;
	for(int i=0; i<sz(R); i++){
		while(ptr < sz(L) && L[ptr].first <= R[i].first){
			baz += foo[L[ptr].second + 1];
			ptr++;
		}
		ret += baz * bar[R[i].second];
	}
	return ret;
}

bool init(){
	memset(val, 0, sizeof(val));
	for(int i=1; i<=n; i++) val[i] = 1;
	for(int i=0; i<n; i++) val[a[i]+1]--;
	for(int i=1; i<=n; i++){
		val[i] += val[i-1];
		if(val[i] <= 0) return 1;
	}
	pref[0] = 1;
	for(int i=1; i<=n; i++){
		pref[i] = pref[i-1] * mint(val[i]);
	}
	suff[n+1] = 1;
	for(int i=n; i; i--){
		suff[i] = suff[i+1] * mint(val[i]);
	}
	return 0;
}

mint solve(){
	if(init()) return 0;
	for(int i=1; i<=n; i++) v[i].clear();
	for(int i=0; i<n; i++){
		v[a[i]].push_back(i);
	}
	return dnc(1, n) / mint(2);
	/*
	for(int i=0; i<n; i++){
		for(int j=0; j<i; j++){
			if(a[j] < a[i]){
				ret += f(j, i);
			}
		}
	}
	*/
}

int main(){
	scanf("%d",&n);
	for(int i=0; i<n; i++) scanf("%d",&a[i]);
	if(init()){
		puts("0");
		return 0;
	}
	mint ret = 0;
	{
		mint inv = 0;
		for(int i=n-1; i>=0; i--){
			inv += mint(bit.query(a[i]) + bit.query(a[i] - 1));
			bit.add(a[i], 1);
		}
		ret += inv * f(-69, -69);
	}
	ret += solve();
	reverse(a, a + n);
	ret -= solve();
	cout << ret << endl;
}

```