#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& x : v)
    is >> x;
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

const int P = 1000000007;

int norm(int x) { return x >= P ? x - P : x; }

struct Node {
  int l, r, v, tag;
  Node *ls, *rs;

  int get() const { return v * (ll)tag % P; }

  void pd() {
    if (tag != 1) {
      v = v * (ll)tag % P;
      ls->tag = ls->tag * (ll)tag % P;
      rs->tag = rs->tag * (ll)tag % P;
      tag = 1;
    }
  }

  void upd() {
    v = norm(ls->get() + rs->get());
  }

  int qry(int k) {
    if (r == k) return get();
    pd();
    if (k <= ls->r) return ls->qry(k);
    return norm(ls->get() + rs->qry(k));
  }

  void ch(int k, int x) {
    if (l == r) {
      tag = 1;
      v = x;
      return;
    }
    pd();
    (k <= ls->r ? ls : rs)->ch(k, x);
    upd();
  }
};

const int N = 200010, INV2 = (P + 1) >> 1;

Node* build(int l, int r) {
  static Node pool[N * 4];
  static Node* top = pool;
  Node* p = top++;
  p->l = l;
  p->r = r;
  p->tag = 1;
  if (l == r) return p;
  int mid = (l + r) >> 1;
  p->ls = build(l, mid);
  p->rs = build(mid + 1, r);
  return p;
}

int main() {
#ifdef LBT
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<int> a(n);
  cin >> a;
  vector<pair<int, int>> sorted(n);
  for (int i = 0; i < n; ++i)
    sorted[i] = make_pair(a[i], i);
  sort(sorted.begin(), sorted.end());
  int tot = 1;
  for (int i = 0; i < n; ++i)
    tot = tot * (ll)(sorted[i].first - i) % P;
  if (tot == 0) {
    cout << 0;
  } else {
    int ans = 0;
    vector<int> pos(n);
    for (int i = 0; i < n; ++i)
      pos[sorted[i].second] = i;
    vector<int> inv(n + 1);
    inv[1] = 1;
    for (int i = 2; i <= n; ++i)
      inv[i] = -(P / i) * (ll)inv[P % i] % P + P;
    Node* segTree = build(0, n - 1), *segTree2 = build(0, n - 1);
    for (int i = n - 1; i >= 0; --i) {
      int left = segTree->qry(sorted[i].second), right = norm(segTree->get() + P - left);
      int lc = segTree2->qry(sorted[i].second);
      segTree->tag = segTree->tag * (sorted[i].first - i - 1LL) % P * inv[sorted[i].first - i] % P;
      segTree->ch(sorted[i].second, inv[sorted[i].first - i]);
      segTree2->ch(sorted[i].second, 1);
      ans = (ans + right * (ll)tot % P * (sorted[i].first - i - 1) % P * INV2) % P;
      ans = (ans + (P - left) * (ll)tot % P * (sorted[i].first - i - 1) % P * INV2) % P;
      ans = (ans + lc * (ll)tot) % P;
    }
    cout << ans;
  }

#ifdef LBT
  LOG("Time: %dms\n", int ((clock()
          -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}

### Prompt

In Cpp, your task is to solve the following problem:

Snuke has an integer sequence A whose length is N. He likes permutations of (1, 2, ..., N), P, that satisfy the following condition:

* P_i \leq A_i for all i ( 1 \leq i \leq N ).



Snuke is interested in the inversion numbers of such permutations. Find the sum of the inversion numbers over all permutations that satisfy the condition. Since this can be extremely large, compute the sum modulo 10^9+7.

Constraints

* 1 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N ( 1 \leq i \leq N )
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
A_1 A_2 ... A_N


Output

Print the sum of the inversion numbers over all permutations that satisfy the condition.

Examples

Input

3
2 3 3


Output

4


Input

6
4 2 5 1 6 3


Output

7


Input

5
4 4 4 4 4


Output

0


Input

30
22 30 15 20 10 29 11 29 28 11 26 10 18 28 22 5 29 16 24 24 27 10 21 30 29 19 28 27 18 23


Output

848414012

### Response

```cpp
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& x : v)
    is >> x;
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

const int P = 1000000007;

int norm(int x) { return x >= P ? x - P : x; }

struct Node {
  int l, r, v, tag;
  Node *ls, *rs;

  int get() const { return v * (ll)tag % P; }

  void pd() {
    if (tag != 1) {
      v = v * (ll)tag % P;
      ls->tag = ls->tag * (ll)tag % P;
      rs->tag = rs->tag * (ll)tag % P;
      tag = 1;
    }
  }

  void upd() {
    v = norm(ls->get() + rs->get());
  }

  int qry(int k) {
    if (r == k) return get();
    pd();
    if (k <= ls->r) return ls->qry(k);
    return norm(ls->get() + rs->qry(k));
  }

  void ch(int k, int x) {
    if (l == r) {
      tag = 1;
      v = x;
      return;
    }
    pd();
    (k <= ls->r ? ls : rs)->ch(k, x);
    upd();
  }
};

const int N = 200010, INV2 = (P + 1) >> 1;

Node* build(int l, int r) {
  static Node pool[N * 4];
  static Node* top = pool;
  Node* p = top++;
  p->l = l;
  p->r = r;
  p->tag = 1;
  if (l == r) return p;
  int mid = (l + r) >> 1;
  p->ls = build(l, mid);
  p->rs = build(mid + 1, r);
  return p;
}

int main() {
#ifdef LBT
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<int> a(n);
  cin >> a;
  vector<pair<int, int>> sorted(n);
  for (int i = 0; i < n; ++i)
    sorted[i] = make_pair(a[i], i);
  sort(sorted.begin(), sorted.end());
  int tot = 1;
  for (int i = 0; i < n; ++i)
    tot = tot * (ll)(sorted[i].first - i) % P;
  if (tot == 0) {
    cout << 0;
  } else {
    int ans = 0;
    vector<int> pos(n);
    for (int i = 0; i < n; ++i)
      pos[sorted[i].second] = i;
    vector<int> inv(n + 1);
    inv[1] = 1;
    for (int i = 2; i <= n; ++i)
      inv[i] = -(P / i) * (ll)inv[P % i] % P + P;
    Node* segTree = build(0, n - 1), *segTree2 = build(0, n - 1);
    for (int i = n - 1; i >= 0; --i) {
      int left = segTree->qry(sorted[i].second), right = norm(segTree->get() + P - left);
      int lc = segTree2->qry(sorted[i].second);
      segTree->tag = segTree->tag * (sorted[i].first - i - 1LL) % P * inv[sorted[i].first - i] % P;
      segTree->ch(sorted[i].second, inv[sorted[i].first - i]);
      segTree2->ch(sorted[i].second, 1);
      ans = (ans + right * (ll)tot % P * (sorted[i].first - i - 1) % P * INV2) % P;
      ans = (ans + (P - left) * (ll)tot % P * (sorted[i].first - i - 1) % P * INV2) % P;
      ans = (ans + lc * (ll)tot) % P;
    }
    cout << ans;
  }

#ifdef LBT
  LOG("Time: %dms\n", int ((clock()
          -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}

```