#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;
}

Measured memory_bytes: 4992