#include <bits/stdc++.h>
using namespace std;
#define ri rd<int>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; --i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++i)
const int maxN = 2e5 + 7;
typedef long long LL;
const LL O = 1e9 + 7;

template<class T> T rd() {
	bool f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
	T x = 0; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return f ? x : -x;
}

int n;
struct Node {
	int v, i;
	inline bool operator < (const Node &y) const {
		return v > y.v;
	}
}a[maxN];
LL c[maxN], d[maxN];
LL C[maxN], sufC[maxN];

namespace Seg {
	const int maxN = ::maxN << 1;
	struct Node;
	typedef Node* node;
	struct Node {
		node lc, rc;
		int l, mid, r;
		int sz;
		LL sum, tag;
		bool havtag;
		Node(int _sz = 0, LL _sum = 0) : sz(_sz), sum(_sum) { havtag = false; }

		Node operator + (const Node &v) const {
			return Node(sz + v.sz, (sum + v.sum) % O);
		}

		void totag(LL d) {
			(sum *= d) %= O;
			if (!havtag) havtag = true, tag = 1;
			(tag *= d) %= O;
		}

		void pushdown() {
			if (havtag) {
				lc->totag(tag);
				rc->totag(tag);
				havtag = false;
			}
		}
	}*rt, pool[maxN], *tpool = pool;

	void build(node &x, int l, int r) {
		x = tpool++;
		x->l = l, x->r = r, x->mid = (l + r) >> 1;
		if (l == r) return;
		build(x->lc, l, x->mid);
		build(x->rc, x->mid+1, r);
	}

	void init() {
		build(rt, 1, n);
	}

	void ins(node x, int to, LL d) {
		x->sz++;
		(x->sum += d) %= O;
		if (x->l == x->r) return;
		x->pushdown();
		if (to <= x->mid) ins(x->lc, to, d);
		else ins(x->rc, to, d);
	}

	void ins(int x, LL d) {
		ins(rt, x, d);
	}

	Node get(node x, int l, int r) {
		if (l <= x->l && x->r <= r) return *x;
		x->pushdown();
		if (r <= x->mid) return get(x->lc, l, r);
		if (x->mid < l) return get(x->rc, l, r);
		return get(x->lc, l, x->mid) + get(x->rc, x->mid+1, r);
	}

	Node get(int l, int r) {
		if (l > r) return Node();
		return get(rt, l, r);
	}
}

int main() {

	n = ri();
	rep (i, 1, n) {
		c[a[i].v = ri()]++;
		a[i].i = i;
	}

	per (i, n, 1) c[i] += c[i+1];
	rep (i, 1, n) {
		c[i] = c[i] - (n-i);
		d[i] = c[i] - 1;
	}
	sufC[n] = 1; per (i, n-1, 0) sufC[i] = sufC[i+1] * c[i+1] % O;
	C[0] = 1; rep (i, 1, n) C[i] = C[i-1] * c[i] % O;

	Seg::init();
	sort(a+1, a+n+1);
	LL ans = 0, cnt = 0;
	for (int v = n, i = 1; v; --v) {
		LL res = 0;
		for (; i <= n && a[i].v == v; ++i) {
			Seg::Node tp = Seg::get(1, a[i].i-1);
			res -= tp.sum;
			cnt += tp.sz;
			tp = Seg::get(a[i].i+1, n);
			res += tp.sum;
			Seg::ins(a[i].i, sufC[v]);
		}
		ans += res % O * C[v] % O;
		Seg::rt->totag(d[v]);
	}
	ans = ans % O * (O+1) / 2 % O;
	ans += cnt % O * C[n] % O;
	printf("%lld\n", (ans % O + O) % O);

	return 0;
}
