#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n)                                      \
  cerr << __LINE__ << ": " << #x << " = {";                   \
  for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \
    cerr << ((hoge) ? "," : "") << x[hoge];                   \
  cerr << "}" << '\n'
#define debugMatrix(x, h, w)                                         \
  cerr << __LINE__ << ": " << #x << " =\n";                          \
  for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) {      \
    cerr << ((hoge ? " {" : "{{"));                                  \
    for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga))      \
      cerr << ((fuga ? ", " : "")) << x[hoge][fuga];                 \
    cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n'; \
  }
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif

template <int mod>
struct ModInt {
  int64_t x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) { return *this *= p.inverse(); }
  ModInt operator-() const { return ModInt() - *this; }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b) t = a / b, swap(a -= t * b, b), swap(u -= t * v, v);
    return ModInt(u);
  }
  ModInt pow(int64_t e) const {
    ModInt ret(1);
    for (ModInt b = *this; e; e >>= 1, b *= b)
      if (e & 1) ret *= b;
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int modulo() { return mod; }
  int get() const { return x; }
};

template <typename T>
struct BinaryIndexedTree {
  vector<T> dat;
  BinaryIndexedTree(int n) : dat(n + 1, 0) {}
  BinaryIndexedTree(int n, T a) : BinaryIndexedTree(vector<T>(n, a)) {}
  BinaryIndexedTree(vector<T> y) : dat(y.size() + 1, 0) {
    for (size_t i = 0; i < y.size(); ++i) dat[i + 1] = y[i];
    for (int i = 1; i < (int)dat.size(); ++i)
      if (i + (i & -i) < (int)dat.size()) dat[i + (i & -i)] += dat[i];
  }
  void add(int i, T a = 1) {
    for (++i; i < (int)dat.size(); i += i & -i) dat[i] += a;
  }
  T sum(int i) {  // sum [0,i)
    T s = 0;
    for (; i > 0; i &= i - 1) s += dat[i];
    return s;
  }
  // sum [l,r)
  T sum(int l, int r) { return sum(r) - sum(l); }
  T operator[](size_t k) { return sum(k + 1) - sum(k); }
  // min { i : sum(i+1) > k } -> kth element(0-indexed)
  int find(T k) const {
    int i = 0;
    for (int p = 1 << (__lg(dat.size() - 1) + 1); p > 0; p >>= 1)
      if (i + p < (int)dat.size() && dat[i + p] <= k) k -= dat[i += p];
    return i + 1 == (int)dat.size() ? -1 : i;  // -1 -> no solutions
  }
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  using Mint = ModInt<int(1e9 + 7)>;
  int N;
  cin >> N;
  int A[N], cnt[N];
  fill(cnt, cnt + N, 0);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    cnt[--A[i]]++;
  }
  for (int k = N - 2; k >= 0; k--) cnt[k] += cnt[k + 1];
  for (int k = 0; k < N; k++) cnt[k] -= (N - 1 - k);
  Mint S = 1;
  for (int k = 0; k < N; k++) S *= cnt[k];
  //   debugArray(cnt, N);
  if (S == 0) {
    cout << 0 << '\n';
    return 0;
  }
  int till[N];
  till[N - 1] = N;
  for (int i = N - 2; i >= 0; i--) {
    if (cnt[i + 1] == 1) {
      till[i] = i + 1;
    } else {
      till[i] = till[i + 1];
    }
  }
  //   debugArray(till, N);
  Mint D[N];
  for (int k = 0; k < N; k++)
    D[k] = (cnt[k] == 1 ? 1 : Mint(cnt[k] - 1) / Mint(cnt[k]))
           * (k ? D[k - 1] : 1);
  //   debugArray(D, N);
  Mint ans = 0;
  {
    BinaryIndexedTree<Mint> bit(N);
    for (int i = N - 1; i >= 0; i--) {
      int x = A[i];
      ans += S * (bit.sum(x, till[x])) / D[x] / 2;
      bit.add(x, D[x]);
    }
  }
  //   debug(ans);
  {
    BinaryIndexedTree<Mint> bit(N), bit2(N);
    for (int i = 0; i < N; i++) {
      int x = A[i];
      ans += S * (bit2.sum(x + 1, N) - bit.sum(x + 1, till[x]) / D[x] / 2);
      bit.add(x, D[x]);
      bit2.add(x, 1);
    }
  }
  cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 10172