#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}

const int N = 2e5 + 7;
const int P = 1e9 + 7;
const int Inv2 = (P + 1) / 2;

#define lowbit(x) ((x) & -(x))

int n, s, ans;
int a[N], b[N], sr[N], ss[N];

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
	int ans = 1;
	for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
	return ans;
}

struct BIT {
	int s[N];
	inline void cls() { memset(s, 0, sizeof(*s) * (n + 1)); }
	inline void qadd(int x, int k) { /*dbg("qadd: x = %d, k = %d\n", x, k);*/ for (; x <= n; x += lowbit(x)) sadd(s[x], k); }
	inline int qsum(int x) {
		int ans = 0;
		for (; x; x -= lowbit(x)) sadd(ans, s[x]);
		return ans;
	}
	inline int qsum(int l, int r) { /*dbg("qsum: l = %d, r = %d\n", l, r);*/ return smod(qsum(r) + P - qsum(l - 1)); }
}sa, sb;

inline void ycl() {
	for (int i = n; i; --i) b[i] += b[i + 1];//, dbg("i = %d, b[i] = %d\n", i, b[i]);
	s = 1;
	for (int i = n; i; --i) { 
		ss[i] = b[i] - (n - i);
		s = (ll)s * ss[i] % P;
		if (i == n || ss[i + 1] == 1) sr[i] = i;
		else sr[i] = sr[i + 1];
		// dbg("i = %d, b[i] = %d, ss[i] = %d, sr[i] = %d\n", i, b[i], ss[i], sr[i]);
	}
	ss[0] = 1;
	for (int i = 1; i <= n; ++i)
		if (ss[i] == 1) ss[i] = ss[i - 1];
		else ss[i] = (ll)ss[i - 1] * (ss[i] - 1) % P * fpow(ss[i], P - 2) % P;
}

inline void work() {
	ycl();
	for (int i = n; i; --i) {
		sadd(ans, (ll)s * sa.qsum(a[i], sr[a[i]]) % P * fpow(ss[a[i]], P - 2) % P);
		// dbg("** i = %d, ans = %d, a[i] = %d, ss[i] = %d, %d %d\n", i, ans, a[i], ss[i], sa.qsum(a[i], sr[a[i]]), fpow(ss[a[i]], P - 2));
		sa.qadd(a[i], ss[a[i]]);
	}
	sa.cls();
	for (int i = 1; i <= n; ++i) {
		sadd(ans, (2ll * s * sb.qsum(a[i] + 1, n) + (ll)(P - s) * sa.qsum(a[i] + 1, sr[a[i]]) % P * fpow(ss[a[i]], P - 2)) % P);
		sa.qadd(a[i], ss[a[i]]), sb.qadd(a[i], 1);
		// dbg("***** i = %d, ans = %d, a[i] = %d, ss[i] = %d, %d, %d %d\n", i, ans, a[i], ss[i], sb.qsum(a[i] + 1, sr[a[i]]), sa.qsum(a[i], sr[a[i]]), fpow(ss[a[i]], P - 2));
	}
	ans = (ll)ans * Inv2 % P;
	printf("%d\n", ans);
}

inline void init() {
	read(n);
	for (int i = 1; i <= n; ++i) read(a[i]), ++b[a[i]];
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}
### Prompt

Generate a Cpp solution to the following problem:

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 fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}

const int N = 2e5 + 7;
const int P = 1e9 + 7;
const int Inv2 = (P + 1) / 2;

#define lowbit(x) ((x) & -(x))

int n, s, ans;
int a[N], b[N], sr[N], ss[N];

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
	int ans = 1;
	for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
	return ans;
}

struct BIT {
	int s[N];
	inline void cls() { memset(s, 0, sizeof(*s) * (n + 1)); }
	inline void qadd(int x, int k) { /*dbg("qadd: x = %d, k = %d\n", x, k);*/ for (; x <= n; x += lowbit(x)) sadd(s[x], k); }
	inline int qsum(int x) {
		int ans = 0;
		for (; x; x -= lowbit(x)) sadd(ans, s[x]);
		return ans;
	}
	inline int qsum(int l, int r) { /*dbg("qsum: l = %d, r = %d\n", l, r);*/ return smod(qsum(r) + P - qsum(l - 1)); }
}sa, sb;

inline void ycl() {
	for (int i = n; i; --i) b[i] += b[i + 1];//, dbg("i = %d, b[i] = %d\n", i, b[i]);
	s = 1;
	for (int i = n; i; --i) { 
		ss[i] = b[i] - (n - i);
		s = (ll)s * ss[i] % P;
		if (i == n || ss[i + 1] == 1) sr[i] = i;
		else sr[i] = sr[i + 1];
		// dbg("i = %d, b[i] = %d, ss[i] = %d, sr[i] = %d\n", i, b[i], ss[i], sr[i]);
	}
	ss[0] = 1;
	for (int i = 1; i <= n; ++i)
		if (ss[i] == 1) ss[i] = ss[i - 1];
		else ss[i] = (ll)ss[i - 1] * (ss[i] - 1) % P * fpow(ss[i], P - 2) % P;
}

inline void work() {
	ycl();
	for (int i = n; i; --i) {
		sadd(ans, (ll)s * sa.qsum(a[i], sr[a[i]]) % P * fpow(ss[a[i]], P - 2) % P);
		// dbg("** i = %d, ans = %d, a[i] = %d, ss[i] = %d, %d %d\n", i, ans, a[i], ss[i], sa.qsum(a[i], sr[a[i]]), fpow(ss[a[i]], P - 2));
		sa.qadd(a[i], ss[a[i]]);
	}
	sa.cls();
	for (int i = 1; i <= n; ++i) {
		sadd(ans, (2ll * s * sb.qsum(a[i] + 1, n) + (ll)(P - s) * sa.qsum(a[i] + 1, sr[a[i]]) % P * fpow(ss[a[i]], P - 2)) % P);
		sa.qadd(a[i], ss[a[i]]), sb.qadd(a[i], 1);
		// dbg("***** i = %d, ans = %d, a[i] = %d, ss[i] = %d, %d, %d %d\n", i, ans, a[i], ss[i], sb.qsum(a[i] + 1, sr[a[i]]), sa.qsum(a[i], sr[a[i]]), fpow(ss[a[i]], P - 2));
	}
	ans = (ll)ans * Inv2 % P;
	printf("%d\n", ans);
}

inline void init() {
	read(n);
	for (int i = 1; i <= n; ++i) read(a[i]), ++b[a[i]];
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}
```