#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void chmin(A &a, B b) {
  if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A &a, B b) {
  if (a < b) a = b;
}
const long long mod = 1000000007;
inline void add(long long &a, long long b) {
  a += b;
  if (a >= mod) a -= mod;
}
long long mpow(long long n, long long m) {
  long long ret = 1;
  while (m) {
    if (m & 1) ret = ret * n % mod;
    n = n * n % mod;
    m >>= 1;
  }
  return ret;
}
const long long FACT_SIZE = 1111111;
long long fact[FACT_SIZE];
long long inv[FACT_SIZE];
struct fact_exec {
  fact_exec() {
    fact[0] = 1;
    for (long long i = 1; i < FACT_SIZE; i++) fact[i] = fact[i - 1] * i % mod;
    inv[FACT_SIZE - 1] = mpow(fact[FACT_SIZE - 1], mod - 2);
    for (long long i = FACT_SIZE - 2; i >= 0; i--)
      inv[i] = inv[i + 1] * (i + 1) % mod;
  }
} factexec;
long long nCk(long long n, long long k) {
  if (n < 0 | k < 0 || k > n) return 0;
  return fact[n] * inv[k] % mod * inv[n - k] % mod;
}
long long nPk(long long n, long long k) {
  if (n < 0 || k < 0 || k > n) return 0;
  return fact[n] * inv[n - k] % mod;
}
struct BinaryIndexedTree {
  long long n;
  vector<long long> dat;
  BinaryIndexedTree(long long n = 0) : n(n) { dat.resize(n + 1); }
  void add(long long k, long long x) {
    for (k++; k <= n; k += k & -k) dat[k] += x;
  }
  long long sum(long long k) {
    long long ret = 0;
    for (k++; k; k -= k & -k) ret += dat[k];
    return ret;
  }
};
long long al;
long long solve(vector<long long> A) {
  long long N = A.size();
  vector<long long> S(N);
  for (long long i = 0; i < (N); i++) S[A[i]]++;
  for (long long i = N - 1; i > 0; i--) S[i - 1] += S[i];
  for (long long i = 0; i < (N); i++) S[i] -= N - 1 - i;
  vector<long long> MS = S, MS2 = S;
  for (long long i = 1; i < N; i++) MS2[i]--;
  for (long long i = 1; i < N; i++) {
    MS[i] = MS[i] * MS[i - 1] % mod;
    MS2[i] = MS2[i] * MS2[i - 1] % mod;
  }
  BinaryIndexedTree bit(N);
  BinaryIndexedTree bit2(N);
  BinaryIndexedTree bit3(N);
  long long ret = 0;
  for (long long i = 0; i < N; i++) {
    long long tmp = bit.sum(A[i]);
    tmp = tmp * MS2[A[i]] % mod * mpow(MS[A[i]], mod - 2) % mod;
    tmp = tmp * inv[2] % mod;
    add(ret, tmp * al % mod);
    tmp = bit2.sum(N - 1) - bit2.sum(A[i]) + mod;
    tmp %= mod;
    tmp = tmp * MS[A[i]] % mod * mpow(MS2[A[i]], mod - 2) % mod;
    tmp = tmp * al % mod;
    add(ret, tmp * inv[2] % mod);
    add(ret, (al * (i - bit3.sum(A[i])) % mod - tmp + mod) % mod);
    bit.add(A[i], MS[A[i]] * mpow(MS2[A[i]], mod - 2) % mod);
    bit2.add(A[i], MS2[A[i]] * mpow(MS[A[i]], mod - 2) % mod);
    bit3.add(A[i], 1);
  }
  return ret;
}
signed main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (long long i = 0; i < (N); i++) cin >> A[i], A[i]--;
  vector<long long> S(N);
  for (long long i = 0; i < (N); i++) S[A[i]]++;
  for (long long i = N - 1; i > 0; i--) S[i - 1] += S[i];
  for (long long i = 0; i < (N); i++) S[i] -= N - 1 - i;
  al = 1;
  for (long long i = 0; i < (N); i++) al = al * S[i] % mod;
  if (*min_element((S).begin(), (S).end()) <= 0) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = 0;
  vector<vector<long long> > lis(N);
  for (long long i = 0; i < (N); i++) lis[A[i]].push_back(i);
  BinaryIndexedTree bit(N);
  long long pre = N;
  for (long long i = N - 1; i >= 0; i--) {
    if (S[i] != 1) continue;
    vector<long long> idx;
    for (long long j = i; j < pre; j++)
      for (auto k : lis[j]) idx.push_back(k);
    sort((idx).begin(), (idx).end());
    for (auto k : idx) add(ans, bit.sum(k) * al % mod);
    for (auto k : idx) bit.add(k, 1);
    vector<long long> a(idx.size());
    for (long long j = 0; j < (idx.size()); j++) a[j] = A[idx[j]] - i;
    add(ans, solve(a));
    pre = i;
  }
  cout << ans << endl;
  return 0;
}
