#include <cstdio>
#include <cassert>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;

#define fi first
#define se second

#define lf else if

namespace io
{
	const int maxb = 1 << 15;
	char b[maxb], *s = b, *t = b;
	bool Getchar(char &ch)
	{
		return ch = s == t && (t = (s = b) + fread(b, 1, maxb, stdin)) == b ? 0 : *s ++;
	}
	int Getint()
	{
		char ch;
		while (Getchar(ch) && (ch < '0' || ch > '9'));
		int s = ch - '0';
		while (Getchar(ch) && ch >= '0' && ch <= '9')
			s = s * 10 + ch - '0';
		return s;
	}
}

using io::Getint;

const int maxn = 2e5 + 10;
const int Mod = 1e9 + 7;
const int inv2 = (Mod + 1) / 2;

void Chkadd(int &x, const int &y) { if ((x += y) >= Mod) x -= Mod; }
void Chksub(int &x, const int &y) { if ((x -= y) < 0) x += Mod; }

int Power(int a, int k)
{
	int x = 1;
	for (; k; k >>= 1)
	{
		if (k & 1) x = 1ll * x * a % Mod;
		a = 1ll * a * a % Mod;
	}
	return x;
}

int n;
int A[maxn], cnt[maxn], S;
PII D[maxn], tree[maxn], iD[maxn];

PII operator * (const PII &a, const int &b)
{
	return b ? PII(1ll * a.fi * b % Mod, a.se) : PII(a.fi, a.se + 1);
}

void operator += (PII &a, const PII &b)
{
	if (a.se < b.se) a = b;
	lf (a.se == b.se) Chkadd(a.fi, b.fi);
}

void BIT_init()
{
	for (int i = 1; i <= n; ++ i) tree[i] = PII(0, 0);
}

void Update(int x, PII del)
{
	for (; x <= n; x += x & -x) tree[x] += del;
}

PII Query(int x)
{
	PII res(0, 0);
	if (!x) return res;
	for (; x ; x -= x & -x) res += tree[x];
	return res;
}

int tr[maxn];

void Insert(int x)
{
	for (; x <= n; x += x & -x) ++ tr[x];
}

int Getsum(int x)
{
	if (!x) return 0;
	int res = 0;
	for (; x; x -= x & (-x)) res += tr[x];
	return res;
}

int main()
{
	n = Getint();
	for (int i = 1; i <= n; ++ i)
	{
		A[i] = Getint();
		++ cnt[A[i]];
	}
	for (int i = n; i; -- i) cnt[i] += cnt[i + 1];
	S = 1;
	for (int i = n; i; -- i)
	{
		cnt[i] -= n - i;
		if (cnt[i] <= 0) return puts("0"), 0;
		S = 1ll * S * cnt[i] % Mod;
	}
	D[0] = PII(1, 0);
	for (int i = 1; i <= n; ++ i)
	{
		D[i] = D[i - 1] * (1ll * (cnt[i] - 1) * Power(cnt[i], Mod - 2) % Mod);
		iD[i] = PII(Power(D[i].fi, Mod - 2), D[i].se);
	}
	
	int ans = 0;
	
	for (int i = 1; i <= n; ++ i)
	{
		int cur = A[i];
		PII res = Query(cur);
		assert(res.se <= D[cur].se);
		if (res.se == D[cur].se)
			Chkadd(ans, 1ll * D[cur].fi * res.fi % Mod);
		Update(cur, iD[cur]);
	}
	
	BIT_init();
	
	for (int i = n; i; -- i)
	{
		int cur = A[i];
		PII res = Query(cur - 1);
		assert(res.se <= D[cur].se);
		if (res.se == D[cur].se)
			Chksub(ans, 1ll * D[cur].fi * res.fi % Mod);
		Update(cur, iD[cur]);
		
		Chkadd(ans, 2ll * Getsum(cur - 1) % Mod);
		Insert(cur);
	}
	
	ans = 1ll * ans * S % Mod * inv2 % Mod;
	
	printf("%lld\n", ans);
	
	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 <cstdio>
#include <cassert>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;

#define fi first
#define se second

#define lf else if

namespace io
{
	const int maxb = 1 << 15;
	char b[maxb], *s = b, *t = b;
	bool Getchar(char &ch)
	{
		return ch = s == t && (t = (s = b) + fread(b, 1, maxb, stdin)) == b ? 0 : *s ++;
	}
	int Getint()
	{
		char ch;
		while (Getchar(ch) && (ch < '0' || ch > '9'));
		int s = ch - '0';
		while (Getchar(ch) && ch >= '0' && ch <= '9')
			s = s * 10 + ch - '0';
		return s;
	}
}

using io::Getint;

const int maxn = 2e5 + 10;
const int Mod = 1e9 + 7;
const int inv2 = (Mod + 1) / 2;

void Chkadd(int &x, const int &y) { if ((x += y) >= Mod) x -= Mod; }
void Chksub(int &x, const int &y) { if ((x -= y) < 0) x += Mod; }

int Power(int a, int k)
{
	int x = 1;
	for (; k; k >>= 1)
	{
		if (k & 1) x = 1ll * x * a % Mod;
		a = 1ll * a * a % Mod;
	}
	return x;
}

int n;
int A[maxn], cnt[maxn], S;
PII D[maxn], tree[maxn], iD[maxn];

PII operator * (const PII &a, const int &b)
{
	return b ? PII(1ll * a.fi * b % Mod, a.se) : PII(a.fi, a.se + 1);
}

void operator += (PII &a, const PII &b)
{
	if (a.se < b.se) a = b;
	lf (a.se == b.se) Chkadd(a.fi, b.fi);
}

void BIT_init()
{
	for (int i = 1; i <= n; ++ i) tree[i] = PII(0, 0);
}

void Update(int x, PII del)
{
	for (; x <= n; x += x & -x) tree[x] += del;
}

PII Query(int x)
{
	PII res(0, 0);
	if (!x) return res;
	for (; x ; x -= x & -x) res += tree[x];
	return res;
}

int tr[maxn];

void Insert(int x)
{
	for (; x <= n; x += x & -x) ++ tr[x];
}

int Getsum(int x)
{
	if (!x) return 0;
	int res = 0;
	for (; x; x -= x & (-x)) res += tr[x];
	return res;
}

int main()
{
	n = Getint();
	for (int i = 1; i <= n; ++ i)
	{
		A[i] = Getint();
		++ cnt[A[i]];
	}
	for (int i = n; i; -- i) cnt[i] += cnt[i + 1];
	S = 1;
	for (int i = n; i; -- i)
	{
		cnt[i] -= n - i;
		if (cnt[i] <= 0) return puts("0"), 0;
		S = 1ll * S * cnt[i] % Mod;
	}
	D[0] = PII(1, 0);
	for (int i = 1; i <= n; ++ i)
	{
		D[i] = D[i - 1] * (1ll * (cnt[i] - 1) * Power(cnt[i], Mod - 2) % Mod);
		iD[i] = PII(Power(D[i].fi, Mod - 2), D[i].se);
	}
	
	int ans = 0;
	
	for (int i = 1; i <= n; ++ i)
	{
		int cur = A[i];
		PII res = Query(cur);
		assert(res.se <= D[cur].se);
		if (res.se == D[cur].se)
			Chkadd(ans, 1ll * D[cur].fi * res.fi % Mod);
		Update(cur, iD[cur]);
	}
	
	BIT_init();
	
	for (int i = n; i; -- i)
	{
		int cur = A[i];
		PII res = Query(cur - 1);
		assert(res.se <= D[cur].se);
		if (res.se == D[cur].se)
			Chksub(ans, 1ll * D[cur].fi * res.fi % Mod);
		Update(cur, iD[cur]);
		
		Chkadd(ans, 2ll * Getsum(cur - 1) % Mod);
		Insert(cur);
	}
	
	ans = 1ll * ans * S % Mod * inv2 % Mod;
	
	printf("%lld\n", ans);
	
	return 0;
}
```