#include <bits/stdc++.h>
using namespace std;
void exgcd(int a, int b, int& x, int& y) {
  if (!b) {
    x = 1, y = 0;
  } else {
    exgcd(b, a % b, y, x);
    y -= (a / b) * x;
  }
}
int inv(int a, int n) {
  int x, y;
  exgcd(a, n, x, y);
  return (x < 0) ? (x + n) : (x);
}
const int Mod = 1e9 + 7;
template <const int Mod = ::Mod>
class Z {
 public:
  int v;
  Z() : v(0) {}
  Z(int x) : v(x) { assert(x >= 0 && x < Mod); }
  Z(long long x) : v(x % Mod) { assert(v >= 0); }
  Z operator+(Z b) {
    int x;
    return Z(((x = v + b.v) >= Mod) ? (x - Mod) : (x));
  }
  Z operator-(Z b) {
    int x;
    return Z(((x = v - b.v) < 0) ? (x + Mod) : (x));
  }
  Z operator*(Z b) { return Z(v * 1ll * b.v); }
  Z operator~() { return inv(v, Mod); }
  Z operator-() { return Z(0) - *this; }
  Z& operator+=(Z b) { return *this = *this + b; }
  Z& operator-=(Z b) { return *this = *this - b; }
  Z& operator*=(Z b) { return *this = *this * b; }
};
const int N = 2e5 + 5;
typedef class FenwickTree {
 public:
  int s;
  Z<>* a;
  vector<pair<int, int> > op;
  FenwickTree() {}
  FenwickTree(int n) : s(n) {
    a = new Z<>[(n + 1)];
    memset(a, 0, sizeof(Z<>) * (n + 1));
  }
  void add(int idx, Z<> v, bool clr = false) {
    if (!clr) {
      op.push_back(pair<int, int>(idx, v.v));
    }
    for (; idx <= s; idx += (idx & (-idx))) a[idx] += v;
  }
  Z<> query(int idx) {
    Z<> rt = 0;
    for (; idx; idx -= (idx & (-idx))) rt += a[idx];
    return rt;
  }
  void clear() {
    for (auto& par : op) {
      add(par.first, -Z<>(par.second), true);
    }
    op.clear();
  }
} FenwickTree;
const Z<> inv2 = (Mod + 1) >> 1;
int n;
pair<int, int> _a[N];
Z<> P[N], F;
int a[N], b[N];
FenwickTree fen, fenc;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    b[a[i]] += 1;
    _a[i] = pair<int, int>(a[i], i);
  }
  for (int i = n - 1; i; i--) {
    b[i] += b[i + 1];
  }
  F = 1;
  for (int i = 1; i <= n; i++) {
    b[i] = b[i] - (n - i);
    F = F * b[i];
    if (b[i] <= 0) {
      puts("0");
      return 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (b[i] == 1) {
      continue;
    }
    if (P[i - 1].v) {
      P[i] = P[i - 1] * Z<>(b[i] - 1) * ~Z<>(b[i]);
    } else {
      P[i] = Z<>(b[i] - 1) * ~Z<>(b[i]);
    }
  }
  sort(_a + 1, _a + n + 1);
  fen = FenwickTree(n);
  fenc = FenwickTree(n);
  Z<> ans = 0;
  for (int i = 1; i <= n; i++) {
    int x = _a[i].first, idx = _a[i].second;
    if (b[x] == 1) {
      fen.clear();
      continue;
    }
    ans += fen.query(idx) * P[x];
    fen.add(idx, ~P[x]);
  }
  fen.clear();
  for (int i = n; i; i--) {
    int x = _a[i].first, idx = _a[i].second;
    if (b[x] == 1) {
      fen.clear();
      ans += fenc.query(idx) * 2;
    } else {
      ans += fenc.query(idx) * 2 - fen.query(idx) * ~P[x];
      fen.add(idx, P[x]);
    }
    fenc.add(idx, 1);
  }
  ans *= F * inv2;
  printf("%d\n", ans.v);
  return 0;
}
