#include <bits/stdc++.h>
using namespace std;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
constexpr long long 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 << "{";
  for (int i = 0; i < (v.size()); i++) {
    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(long long _v = 0) { set_norm(_v % MOD + MOD); }
  M& set_norm(uint _v) {
    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(long long 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 {
    long long 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>;
int main() {
  int N;
  cin >> N;
  V<int> A(N);
  for (int i = 0; i < (N); i++) cin >> A[i];
  V<int> cnt(N + 1);
  for (int i = 0; i < (N); i++) ++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];
  }
  true;
  Mint ans = 0;
  for (int i = 0; i < (N); i++) {
    for (int j = 0; j < (i); j++) {
      if (A[j] <= A[i]) {
        Mint x = all;
        for (int k = A[j] + 1; k <= A[i]; ++k) {
          x *= Mint(cnt[k] - 1) / Mint(cnt[k]);
        }
        ans += x / Mint(2);
      }
    }
  }
  Mint rev = 0;
  for (int i = 0; i < (N); i++) {
    for (int j = 0; j < (i); j++) {
      if (A[j] > A[i]) {
        Mint x = all;
        for (int k = A[i] + 1; k <= A[j]; ++k) {
          x *= Mint(cnt[k] - 1) / Mint(cnt[k]);
        }
        ans += x / Mint(2);
        rev += all - x;
      }
    }
  }
  true;
  true;
  cout << ans + rev << endl;
  return 0;
}
