#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << (x) << endl
using namespace std;

const int N = 200005;
const long long mod = 1000000007ll;

template <class T>
void read (T &x) {
	int sgn = 1;
	char ch;
	x = 0;
	for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ;
	if (ch == '-') ch = getchar(), sgn = -1;
	for (; '0' <= ch && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	x *= sgn;
}

template <class T>
void write (T x) {
	if (x < 0) putchar('-'), write(-x);
	else if (x < 10) putchar(x + '0');
	else write(x / 10), putchar(x % 10 + '0');
}

struct node {
	int id, lim;
	bool operator < (node rhs) const {
		return lim < rhs.lim;
	}
} vec[N];
int n, sgt_cnt[N << 2];
long long inv[N], sgt[N << 2], sgt_tag[N << 2], res = 1ll, ans = 0ll;
bool flag = true;

void build (int l, int r, int now) {
	int mid = l + r >> 1;
	sgt_cnt[now] = 0, sgt[now] = 0ll, sgt_tag[now] = 1ll;
	if (l < r) {
		build(l, mid, now << 1);
		build(mid + 1, r, now << 1 | 1);
	}
}
void pushup (int now) {
	sgt_cnt[now] = sgt_cnt[now << 1] + sgt_cnt[now << 1 | 1];
	sgt[now] = (sgt[now << 1] + sgt[now << 1 | 1]) % mod;
}
void cover (int now, long long t) {
	sgt[now] = sgt[now] * t % mod;
	sgt_tag[now] = sgt_tag[now] * t % mod;
}
void pushdown (int now) {
	cover(now << 1, sgt_tag[now]);
	cover(now << 1 | 1, sgt_tag[now]);
	sgt_tag[now] = 1ll;
}

void change (int pos, int l, int r, int now, long long val) {
	int mid = l + r >> 1;
	if (l == r) sgt_cnt[now] = 1, sgt[now] = val;
	else {
		pushdown(now);
		if (pos <= mid) change(pos, l, mid, now << 1, val);
		else if (pos > mid) change(pos, mid + 1, r, now << 1 | 1, val);
		pushup(now);
	}
}

int query1 (int left, int right, int l, int r, int now) {
	int mid = l + r >> 1;
	if (l == left && r == right) return sgt_cnt[now];
	else {
		pushdown(now);
		if (right <= mid) return query1(left, right, l, mid, now << 1);
		else if (left > mid) return query1(left, right, mid + 1, r, now << 1 | 1);
		else return query1(left, mid, l, mid, now << 1) + query1(mid + 1, right, mid + 1, r, now << 1 | 1);
	}
}

long long query2 (int left, int right, int l, int r, int now) {
	int mid = l + r >> 1;
	if (l == left && r == right) return sgt[now];
	else {
		pushdown(now);
		if (right <= mid) return query2(left, right, l, mid, now << 1);
		else if (left > mid) return query2(left, right, mid + 1, r, now << 1 | 1);
		else return (query2(left, mid, l, mid, now << 1) + query2(mid + 1, right, mid + 1, r, now << 1 | 1)) % mod;
	}
}

void init () {
	inv[1] = 1ll;
	for (int i = 2; i <= max(n, 2); i++) inv[i] = (mod / i) * (mod - inv[mod % i]) % mod;
}

void solve () {
	build(1, n, 1);
	for (int i = 0; i < n; i++) {
		int cnt = query1(vec[i].id, n, 1, n, 1);
		long long l = query2(1, vec[i].id, 1, n, 1), r = query2(vec[i].id, n, 1, n, 1);
		l = l * res % mod * inv[vec[i].lim - i] % mod;
		r = r * res % mod * inv[vec[i].lim - i] % mod;
		ans = (ans + res * cnt + l - r + mod) % mod;

		sgt_tag[1] = sgt_tag[1] * (vec[i].lim - i - 1) % mod * inv[vec[i].lim - i] % mod;
		sgt[1] = sgt[1] * (vec[i].lim - i - 1) % mod * inv[vec[i].lim - i] % mod;
		change(vec[i].id, 1, n, 1, inv[2] * (vec[i].lim - i - 1) % mod);
	}
}

int main () {
	read(n);
	for (int i = 0; i < n; i++) vec[i].id = i + 1, read(vec[i].lim);

	sort(vec, vec + n);
	for (int i = 0; i < n; i++) {
		if (vec[i].lim <= i) flag = false;
		res = res * (vec[i].lim - i) % mod;
	}

	if (flag) init(), solve();
	write(ans), putchar('\n');
	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 debug(x) cerr << #x << " " << (x) << endl
using namespace std;

const int N = 200005;
const long long mod = 1000000007ll;

template <class T>
void read (T &x) {
	int sgn = 1;
	char ch;
	x = 0;
	for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ;
	if (ch == '-') ch = getchar(), sgn = -1;
	for (; '0' <= ch && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	x *= sgn;
}

template <class T>
void write (T x) {
	if (x < 0) putchar('-'), write(-x);
	else if (x < 10) putchar(x + '0');
	else write(x / 10), putchar(x % 10 + '0');
}

struct node {
	int id, lim;
	bool operator < (node rhs) const {
		return lim < rhs.lim;
	}
} vec[N];
int n, sgt_cnt[N << 2];
long long inv[N], sgt[N << 2], sgt_tag[N << 2], res = 1ll, ans = 0ll;
bool flag = true;

void build (int l, int r, int now) {
	int mid = l + r >> 1;
	sgt_cnt[now] = 0, sgt[now] = 0ll, sgt_tag[now] = 1ll;
	if (l < r) {
		build(l, mid, now << 1);
		build(mid + 1, r, now << 1 | 1);
	}
}
void pushup (int now) {
	sgt_cnt[now] = sgt_cnt[now << 1] + sgt_cnt[now << 1 | 1];
	sgt[now] = (sgt[now << 1] + sgt[now << 1 | 1]) % mod;
}
void cover (int now, long long t) {
	sgt[now] = sgt[now] * t % mod;
	sgt_tag[now] = sgt_tag[now] * t % mod;
}
void pushdown (int now) {
	cover(now << 1, sgt_tag[now]);
	cover(now << 1 | 1, sgt_tag[now]);
	sgt_tag[now] = 1ll;
}

void change (int pos, int l, int r, int now, long long val) {
	int mid = l + r >> 1;
	if (l == r) sgt_cnt[now] = 1, sgt[now] = val;
	else {
		pushdown(now);
		if (pos <= mid) change(pos, l, mid, now << 1, val);
		else if (pos > mid) change(pos, mid + 1, r, now << 1 | 1, val);
		pushup(now);
	}
}

int query1 (int left, int right, int l, int r, int now) {
	int mid = l + r >> 1;
	if (l == left && r == right) return sgt_cnt[now];
	else {
		pushdown(now);
		if (right <= mid) return query1(left, right, l, mid, now << 1);
		else if (left > mid) return query1(left, right, mid + 1, r, now << 1 | 1);
		else return query1(left, mid, l, mid, now << 1) + query1(mid + 1, right, mid + 1, r, now << 1 | 1);
	}
}

long long query2 (int left, int right, int l, int r, int now) {
	int mid = l + r >> 1;
	if (l == left && r == right) return sgt[now];
	else {
		pushdown(now);
		if (right <= mid) return query2(left, right, l, mid, now << 1);
		else if (left > mid) return query2(left, right, mid + 1, r, now << 1 | 1);
		else return (query2(left, mid, l, mid, now << 1) + query2(mid + 1, right, mid + 1, r, now << 1 | 1)) % mod;
	}
}

void init () {
	inv[1] = 1ll;
	for (int i = 2; i <= max(n, 2); i++) inv[i] = (mod / i) * (mod - inv[mod % i]) % mod;
}

void solve () {
	build(1, n, 1);
	for (int i = 0; i < n; i++) {
		int cnt = query1(vec[i].id, n, 1, n, 1);
		long long l = query2(1, vec[i].id, 1, n, 1), r = query2(vec[i].id, n, 1, n, 1);
		l = l * res % mod * inv[vec[i].lim - i] % mod;
		r = r * res % mod * inv[vec[i].lim - i] % mod;
		ans = (ans + res * cnt + l - r + mod) % mod;

		sgt_tag[1] = sgt_tag[1] * (vec[i].lim - i - 1) % mod * inv[vec[i].lim - i] % mod;
		sgt[1] = sgt[1] * (vec[i].lim - i - 1) % mod * inv[vec[i].lim - i] % mod;
		change(vec[i].id, 1, n, 1, inv[2] * (vec[i].lim - i - 1) % mod);
	}
}

int main () {
	read(n);
	for (int i = 0; i < n; i++) vec[i].id = i + 1, read(vec[i].lim);

	sort(vec, vec + n);
	for (int i = 0; i < n; i++) {
		if (vec[i].lim <= i) flag = false;
		res = res * (vec[i].lim - i) % mod;
	}

	if (flag) init(), solve();
	write(ans), putchar('\n');
	return 0;
}
```