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

typedef pair<int, int> pii;
typedef long long ll;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else 
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

template<class T, class U> bool chmax(T& t, const U& u) { if (t < u) { t = u; return 1; } return 0; }
template<class T, class U> bool chmin(T& t, const U& u) { if (t > u) { t = u; return 1; } return 0; }

template<class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os<<"{";
	rep(i, v.size()) {
		if (i) os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

template <unsigned int MOD>
struct ModInt {
	using uint = unsigned int;
	using ull = unsigned long long;
	using M = ModInt;

	uint v;

	ModInt(ll _v = 0) { set_norm(_v % MOD + MOD); }
	M& set_norm(uint _v) { //[0, MOD * 2)->[0, MOD)
		v = (_v < MOD) ? _v : _v - MOD;
		return *this;
	}

	explicit operator bool() const { return v != 0; }
	M operator+(const M& a) const { return M().set_norm(v + a.v); }
	M operator-(const M& a) const { return M().set_norm(v + MOD - a.v); }
	M operator*(const M& a) const { return M().set_norm(ull(v) * a.v % MOD); }
	M operator/(const M& a) const { return *this * a.inv(); }
	M& operator+=(const M& a) { return *this = *this + a; }
	M& operator-=(const M& a) { return *this = *this - a; }
	M& operator*=(const M& a) { return *this = *this * a; }
	M& operator/=(const M& a) { return *this = *this / a; }
	M operator-() const { return M() - *this; }
	M& operator++(int) { return *this = *this + 1; }
	M& operator--(int) { return *this = *this - 1; }

	M pow(ll n) const {
		if (n < 0) return inv().pow(-n);
		M x = *this, res = 1;
		while (n) {
			if (n & 1) res *= x;
			x *= x;
			n >>= 1;
		}
		return res;
	}

	M inv() const {
		ll a = v, b = MOD, p = 1, q = 0, t;
		while (b != 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(p -= t * q, q);
		}
		return M(p);
	}

	bool operator==(const M& a) const { return v == a.v; }
	bool operator!=(const M& a) const { return v != a.v; }
	friend ostream& operator<<(ostream& os, const M& a) { return os << a.v; }
	static int get_mod() { return MOD; }	
};

using Mint = ModInt<1000000007>;

template<class T>
struct BIT {
	int n;
	vector<T> bit;

	BIT(int _n = 0) : n(_n), bit(n + 1) {}

	//sum of [0, i), 0 <= i <= n
	T sum(int i) {
		T s = 0;
		while (i > 0) {
			s += bit[i];
			i -= i & -i;
		}
		return s;
	}

	//0 <= i < n
	void add(int i, T x) {
		++i;
		while (i <= n) {
			bit[i] += x;
			i += i & -i;
		}
	}

	//[l, r) 0 <= l < r < n
	T sum(int l, int r) {
		return sum(r) - sum(l);
	}
};

int main() {
	int N; cin >> N;
	V<int> A(N);
	rep(i, N) cin >> A[i];

	V<int> cnt(N + 1);
	rep(i, N) ++cnt[A[i]];

	for (int i = N-1; i >= 0; --i) {
		cnt[i] += cnt[i+1];
	}

	Mint all = 1;

	for (int i = 1; i <= N; ++i) {
		cnt[i] -= (N - i);
		if (cnt[i] < 0) {
			puts("0");
			return 0;
		}

		all *= cnt[i];
	}

	V<Mint> pref(N + 1);
	V<int> zeros(N + 1);

	pref[0] = 1;
	for (int i = 1; i <= N; ++i) {
		zeros[i] = zeros[i-1] + (cnt[i] == 1);
		Mint val = Mint(cnt[i] - 1) / Mint(cnt[i]);
		pref[i] = (zeros[i] == zeros[i-1] ? pref[i-1] * val : 1);
	}

	dump(all);

	Mint ans = 0;

	BIT<Mint> T(N + 1);

	rep(i, N) { //count j <= i, A_j <= A_i
		int p = A[i];
		int l = lower_bound(ALL(zeros), zeros[p]) - zeros.begin();
		Mint num = all * pref[A[i]] / Mint(2);

		Mint x = T.sum(l, p + 1);
		T.add(p, Mint(1) / pref[p]);
		ans += x * num;
	}

	Mint rev;

	{
		BIT<int> Tinv(N + 1);
		ll invcnt = 0;

		rep(i, N) {
			invcnt += i - Tinv.sum(A[i] + 1);
			Tinv.add(A[i], 1);
		}
		rev = Mint(invcnt) * all;
	}

	BIT<Mint> T2(N + 1);

	rep(i, N) { //count j <= i, A_j > A_i
		int p = A[i];
		int r = upper_bound(ALL(zeros), zeros[p]) - zeros.begin();
		Mint num = all / pref[A[i]] / Mint(2);

		Mint x = T2.sum(p + 1, r);
		T2.add(p, pref[A[i]]);
		rev -= x * num;
	}

	dump(ans);
	dump(rev);
	cout << ans + rev << endl;

	return 0;
}
### Prompt

Construct a Cpp code solution to the problem outlined:

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 pair<int, int> pii;
typedef long long ll;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else 
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

template<class T, class U> bool chmax(T& t, const U& u) { if (t < u) { t = u; return 1; } return 0; }
template<class T, class U> bool chmin(T& t, const U& u) { if (t > u) { t = u; return 1; } return 0; }

template<class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os<<"{";
	rep(i, v.size()) {
		if (i) os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

template <unsigned int MOD>
struct ModInt {
	using uint = unsigned int;
	using ull = unsigned long long;
	using M = ModInt;

	uint v;

	ModInt(ll _v = 0) { set_norm(_v % MOD + MOD); }
	M& set_norm(uint _v) { //[0, MOD * 2)->[0, MOD)
		v = (_v < MOD) ? _v : _v - MOD;
		return *this;
	}

	explicit operator bool() const { return v != 0; }
	M operator+(const M& a) const { return M().set_norm(v + a.v); }
	M operator-(const M& a) const { return M().set_norm(v + MOD - a.v); }
	M operator*(const M& a) const { return M().set_norm(ull(v) * a.v % MOD); }
	M operator/(const M& a) const { return *this * a.inv(); }
	M& operator+=(const M& a) { return *this = *this + a; }
	M& operator-=(const M& a) { return *this = *this - a; }
	M& operator*=(const M& a) { return *this = *this * a; }
	M& operator/=(const M& a) { return *this = *this / a; }
	M operator-() const { return M() - *this; }
	M& operator++(int) { return *this = *this + 1; }
	M& operator--(int) { return *this = *this - 1; }

	M pow(ll n) const {
		if (n < 0) return inv().pow(-n);
		M x = *this, res = 1;
		while (n) {
			if (n & 1) res *= x;
			x *= x;
			n >>= 1;
		}
		return res;
	}

	M inv() const {
		ll a = v, b = MOD, p = 1, q = 0, t;
		while (b != 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(p -= t * q, q);
		}
		return M(p);
	}

	bool operator==(const M& a) const { return v == a.v; }
	bool operator!=(const M& a) const { return v != a.v; }
	friend ostream& operator<<(ostream& os, const M& a) { return os << a.v; }
	static int get_mod() { return MOD; }	
};

using Mint = ModInt<1000000007>;

template<class T>
struct BIT {
	int n;
	vector<T> bit;

	BIT(int _n = 0) : n(_n), bit(n + 1) {}

	//sum of [0, i), 0 <= i <= n
	T sum(int i) {
		T s = 0;
		while (i > 0) {
			s += bit[i];
			i -= i & -i;
		}
		return s;
	}

	//0 <= i < n
	void add(int i, T x) {
		++i;
		while (i <= n) {
			bit[i] += x;
			i += i & -i;
		}
	}

	//[l, r) 0 <= l < r < n
	T sum(int l, int r) {
		return sum(r) - sum(l);
	}
};

int main() {
	int N; cin >> N;
	V<int> A(N);
	rep(i, N) cin >> A[i];

	V<int> cnt(N + 1);
	rep(i, N) ++cnt[A[i]];

	for (int i = N-1; i >= 0; --i) {
		cnt[i] += cnt[i+1];
	}

	Mint all = 1;

	for (int i = 1; i <= N; ++i) {
		cnt[i] -= (N - i);
		if (cnt[i] < 0) {
			puts("0");
			return 0;
		}

		all *= cnt[i];
	}

	V<Mint> pref(N + 1);
	V<int> zeros(N + 1);

	pref[0] = 1;
	for (int i = 1; i <= N; ++i) {
		zeros[i] = zeros[i-1] + (cnt[i] == 1);
		Mint val = Mint(cnt[i] - 1) / Mint(cnt[i]);
		pref[i] = (zeros[i] == zeros[i-1] ? pref[i-1] * val : 1);
	}

	dump(all);

	Mint ans = 0;

	BIT<Mint> T(N + 1);

	rep(i, N) { //count j <= i, A_j <= A_i
		int p = A[i];
		int l = lower_bound(ALL(zeros), zeros[p]) - zeros.begin();
		Mint num = all * pref[A[i]] / Mint(2);

		Mint x = T.sum(l, p + 1);
		T.add(p, Mint(1) / pref[p]);
		ans += x * num;
	}

	Mint rev;

	{
		BIT<int> Tinv(N + 1);
		ll invcnt = 0;

		rep(i, N) {
			invcnt += i - Tinv.sum(A[i] + 1);
			Tinv.add(A[i], 1);
		}
		rev = Mint(invcnt) * all;
	}

	BIT<Mint> T2(N + 1);

	rep(i, N) { //count j <= i, A_j > A_i
		int p = A[i];
		int r = upper_bound(ALL(zeros), zeros[p]) - zeros.begin();
		Mint num = all / pref[A[i]] / Mint(2);

		Mint x = T2.sum(p + 1, r);
		T2.add(p, pref[A[i]]);
		rev -= x * num;
	}

	dump(ans);
	dump(rev);
	cout << ans + rev << endl;

	return 0;
}
```