#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;

#define ll long long

void exgcd(int a, int b, int& x, int& y) {
	if (!b) {
		x = 1, y = 0;
	} else {
		exgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
}

int inv(int a, int n) {
	int x, y;
	exgcd(a, n, x, y);
	return (x < 0) ? (x + n) : (x);
}

const int Mod = 1e9 + 7;

template <const int Mod = :: Mod>
class Z {
	public:
		int v;

		Z() : v(0) {	}
		Z(int x) : v(x){
			assert(x >= 0 && x < Mod);
		}
		Z(ll x) : v(x % Mod) {
			assert(v >= 0);
		}

		Z operator + (Z b) {
			int x;
			return Z(((x = v + b.v) >= Mod) ? (x - Mod) : (x));
		}
		Z operator - (Z b) {
			int x;
			return Z(((x = v - b.v) < 0) ? (x + Mod) : (x));
		}
		Z operator * (Z b) {
			return Z(v * 1ll * b.v);
		}
		Z operator ~() {
			return inv(v, Mod);
		}
		Z operator - () {
			return Z(0) - *this;
		}
		Z& operator += (Z b) {
			return *this = *this + b;
		}
		Z& operator -= (Z b) {
			return *this = *this - b;
		}
		Z& operator *= (Z b) {
			return *this = *this * b;
		}
};

typedef Z<> Zi;

const int N = 2e5 + 5;

#define pii pair<int, int>

typedef class FenwickTree {
	public:
		int s;
		Zi* a;
		vector<pii> op;

		FenwickTree() {	}
		FenwickTree(int n) : s(n) {
			a = new Zi[(n + 1)];
			memset(a, 0, sizeof(Zi) * (n + 1));
		}

		void add(int idx, Zi v, boolean clr = false) {
			if (!clr) {
				op.push_back(pii(idx, v.v));
			}
			for ( ; idx <= s; idx += (idx & (-idx)))
				a[idx] += v;
		}
		Zi query(int idx) {
			Zi rt = 0;
			for ( ; idx; idx -= (idx & (-idx)))
				rt += a[idx];
			return rt;
		}
		void clear() {
			for (auto& par : op) {
				add(par.first, -Zi(par.second), true);
			}
			op.clear();
		}
} FenwickTree;

const Zi inv2 = (Mod + 1) >> 1;

int n;
pii _a[N];
Zi P[N], F;
int a[N], b[N];
FenwickTree fen, fenc;

int main() {
#ifdef local
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", a + i);
		b[a[i]] += 1;
		_a[i] = pii(a[i], i);
	}
	for (int i = n - 1; i; i--) {
		b[i] += b[i + 1];
	}
	F = 1;
	for (int i = 1; i <= n; i++) {
		b[i] = b[i] - (n - i);
		if (b[i] <= 0) {
			puts("0");
			return 0;
		}
		F = F * b[i];
	}
	for (int i = 1; i <= n; i++) {
		if (b[i] == 1) {
			continue;
		}
		if (P[i - 1].v) {
			P[i] = P[i - 1] * Zi(b[i] - 1) * ~Zi(b[i]);
		} else {
			P[i] = Zi(b[i] - 1) * ~Zi(b[i]);
		}
	}
	
	sort(_a + 1, _a + n + 1);
	fen = FenwickTree(n);
	fenc = FenwickTree(n);
	Zi ans = 0;
	int len = 0, lst = 1;
	for (int i = 1; i <= n; i++) {
		int x = _a[i].first, idx = _a[i].second;
		if (i == 1 || x == _a[i - 1].first) {
			len++;
		} else {
			len = 1;
		}
		if (b[x] == 1) {
			lst = x;
			ans += len - 1;
			fen.clear();
			continue;
		}
		while (lst < x) {
			if (b[++lst] == 1) {
				fen.clear();
			}
		}
		ans += fen.query(idx) * P[x];
		fen.add(idx, ~P[x]);
//		cerr << i << " " << (ans * F * inv2).v << '\n';
	}
//	cerr << (ans * F * inv2).v << '\n';
	fen.clear();
	lst = n + 1;
	for (int i = n; i; i--) {
		int x = _a[i].first, idx = _a[i].second;
		if (b[x] == 1) {
			lst = x;
			fen.clear();
			ans += fenc.query(idx) * 2;
		} else {
			while (lst > x) {
				if (b[--lst] == 1) {
					fen.clear();
				}
			}
			ans += fenc.query(idx) * 2 - fen.query(idx) * ~P[x];
			fen.add(idx, P[x]);
		}
		fenc.add(idx, 1);
	}
	ans *= F * inv2;
	printf("%d\n", ans.v);
	return 0;
}

### Prompt

Develop a solution in Cpp 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>
using namespace std;
typedef bool boolean;

#define ll long long

void exgcd(int a, int b, int& x, int& y) {
	if (!b) {
		x = 1, y = 0;
	} else {
		exgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
}

int inv(int a, int n) {
	int x, y;
	exgcd(a, n, x, y);
	return (x < 0) ? (x + n) : (x);
}

const int Mod = 1e9 + 7;

template <const int Mod = :: Mod>
class Z {
	public:
		int v;

		Z() : v(0) {	}
		Z(int x) : v(x){
			assert(x >= 0 && x < Mod);
		}
		Z(ll x) : v(x % Mod) {
			assert(v >= 0);
		}

		Z operator + (Z b) {
			int x;
			return Z(((x = v + b.v) >= Mod) ? (x - Mod) : (x));
		}
		Z operator - (Z b) {
			int x;
			return Z(((x = v - b.v) < 0) ? (x + Mod) : (x));
		}
		Z operator * (Z b) {
			return Z(v * 1ll * b.v);
		}
		Z operator ~() {
			return inv(v, Mod);
		}
		Z operator - () {
			return Z(0) - *this;
		}
		Z& operator += (Z b) {
			return *this = *this + b;
		}
		Z& operator -= (Z b) {
			return *this = *this - b;
		}
		Z& operator *= (Z b) {
			return *this = *this * b;
		}
};

typedef Z<> Zi;

const int N = 2e5 + 5;

#define pii pair<int, int>

typedef class FenwickTree {
	public:
		int s;
		Zi* a;
		vector<pii> op;

		FenwickTree() {	}
		FenwickTree(int n) : s(n) {
			a = new Zi[(n + 1)];
			memset(a, 0, sizeof(Zi) * (n + 1));
		}

		void add(int idx, Zi v, boolean clr = false) {
			if (!clr) {
				op.push_back(pii(idx, v.v));
			}
			for ( ; idx <= s; idx += (idx & (-idx)))
				a[idx] += v;
		}
		Zi query(int idx) {
			Zi rt = 0;
			for ( ; idx; idx -= (idx & (-idx)))
				rt += a[idx];
			return rt;
		}
		void clear() {
			for (auto& par : op) {
				add(par.first, -Zi(par.second), true);
			}
			op.clear();
		}
} FenwickTree;

const Zi inv2 = (Mod + 1) >> 1;

int n;
pii _a[N];
Zi P[N], F;
int a[N], b[N];
FenwickTree fen, fenc;

int main() {
#ifdef local
	freopen("a.in", "r", stdin);
#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", a + i);
		b[a[i]] += 1;
		_a[i] = pii(a[i], i);
	}
	for (int i = n - 1; i; i--) {
		b[i] += b[i + 1];
	}
	F = 1;
	for (int i = 1; i <= n; i++) {
		b[i] = b[i] - (n - i);
		if (b[i] <= 0) {
			puts("0");
			return 0;
		}
		F = F * b[i];
	}
	for (int i = 1; i <= n; i++) {
		if (b[i] == 1) {
			continue;
		}
		if (P[i - 1].v) {
			P[i] = P[i - 1] * Zi(b[i] - 1) * ~Zi(b[i]);
		} else {
			P[i] = Zi(b[i] - 1) * ~Zi(b[i]);
		}
	}
	
	sort(_a + 1, _a + n + 1);
	fen = FenwickTree(n);
	fenc = FenwickTree(n);
	Zi ans = 0;
	int len = 0, lst = 1;
	for (int i = 1; i <= n; i++) {
		int x = _a[i].first, idx = _a[i].second;
		if (i == 1 || x == _a[i - 1].first) {
			len++;
		} else {
			len = 1;
		}
		if (b[x] == 1) {
			lst = x;
			ans += len - 1;
			fen.clear();
			continue;
		}
		while (lst < x) {
			if (b[++lst] == 1) {
				fen.clear();
			}
		}
		ans += fen.query(idx) * P[x];
		fen.add(idx, ~P[x]);
//		cerr << i << " " << (ans * F * inv2).v << '\n';
	}
//	cerr << (ans * F * inv2).v << '\n';
	fen.clear();
	lst = n + 1;
	for (int i = n; i; i--) {
		int x = _a[i].first, idx = _a[i].second;
		if (b[x] == 1) {
			lst = x;
			fen.clear();
			ans += fenc.query(idx) * 2;
		} else {
			while (lst > x) {
				if (b[--lst] == 1) {
					fen.clear();
				}
			}
			ans += fenc.query(idx) * 2 - fen.query(idx) * ~P[x];
			fen.add(idx, P[x]);
		}
		fenc.add(idx, 1);
	}
	ans *= F * inv2;
	printf("%d\n", ans.v);
	return 0;
}

```