Problem p03367 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int mod = 1000000007;
int fpow(int x, int y) {
  if (y == 0)
    return 1;
  int ret = fpow(x, y >> 1);
  ret = (long long)ret * ret % mod;
  if (y & 1)
    ret = (long long)ret * x % mod;
  return ret;
}
int inv(int x) { return fpow(x, mod - 2); }
int cnt[101010];
int h[101010];
int a[101010];
int la[101010];
int sum[101010];
int n;
int lowbit(int x) { return x & (-x); }
void add(int x, int v) {
  for (int i = x; i <= n; i += lowbit(i))
    (sum[i] += v) %= mod;
}
int query(int x) {
  int ret = 0;
  for (int i = x; i; i -= lowbit(i))
    (ret += sum[i]) %= mod;
  return ret;
}
int main() {
  int pn = 1;
  int ans1 = 0, ans2 = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    cnt[a[i]]++;
  }
  for (int i = n - 1; i >= 1; i--)
    cnt[i] += cnt[i + 1];
  for (int i = 1; i <= n; i++)
    cnt[i] -= (n - i);
  for (int i = 1; i <= n; i++)
    if (cnt[i] > 0)
      pn = (long long)pn * cnt[i] % mod;
    else
      pn = 0;
  if (pn == 0) {
    printf("0");
    return 0;
  }
  la[0] = 1;
  h[0] = 1;
  for (int i = 1; i <= n; i++)
    if (cnt[i] > 1) {
      h[i] = (long long)(cnt[i] - 1) * inv(cnt[i]) % mod;
      la[i] = la[i - 1];
    } else {
      h[i] = 1;
      la[i] = i;
    }
  for (int i = 2; i <= n; i++)
    h[i] = (long long)h[i] * h[i - 1] % mod;
  memset(sum, 0, sizeof(sum));
  for (int i = 1; i <= n; i++) {
    ans1 = ((long long)(query(a[i]) - query(la[a[i]] - 1) + mod) * h[a[i]] +
            ans1) %
           mod;
    add(a[i], inv(h[a[i]]));
  }
  ans1 = (long long)ans1 * inv(2) % mod;
  memset(sum, 0, sizeof(sum));
  for (int i = 1; i <= n; i++) {
    (ans1 += i - 1 - query(a[i])) %= mod;
    add(a[i], 1);
  }
  memset(sum, 0, sizeof(sum));
  for (int i = n; i >= 1; i--) {
    ans2 = ((long long)(query(a[i] - 1) - query(la[a[i]] - 1) + mod) * h[a[i]] +
            ans2) %
           mod;
    add(a[i], inv(h[a[i]]));
  }
  ans2 = (long long)ans2 * inv(2) % mod;
  ans1 = (ans1 - ans2 + mod) % mod;
  printf("%lld", (long long)ans1 * pn % mod);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int mod = 1000000007;
int fpow(int x, int y) {
  if (y == 0)
    return 1;
  int ret = fpow(x, y >> 1);
  ret = (long long)ret * ret % mod;
  if (y & 1)
    ret = (long long)ret * x % mod;
  return ret;
}
int inv(int x) { return fpow(x, mod - 2); }
int cnt[202020];
int h[202020];
int a[202020];
int la[202020];
int sum[202020];
int n;
int lowbit(int x) { return x & (-x); }
void add(int x, int v) {
  for (int i = x; i <= n; i += lowbit(i))
    (sum[i] += v) %= mod;
}
int query(int x) {
  int ret = 0;
  for (int i = x; i; i -= lowbit(i))
    (ret += sum[i]) %= mod;
  return ret;
}
int main() {
  int pn = 1;
  int ans1 = 0, ans2 = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    cnt[a[i]]++;
  }
  for (int i = n - 1; i >= 1; i--)
    cnt[i] += cnt[i + 1];
  for (int i = 1; i <= n; i++)
    cnt[i] -= (n - i);
  for (int i = 1; i <= n; i++)
    if (cnt[i] > 0)
      pn = (long long)pn * cnt[i] % mod;
    else
      pn = 0;
  if (pn == 0) {
    printf("0");
    return 0;
  }
  la[0] = 1;
  h[0] = 1;
  for (int i = 1; i <= n; i++)
    if (cnt[i] > 1) {
      h[i] = (long long)(cnt[i] - 1) * inv(cnt[i]) % mod;
      la[i] = la[i - 1];
    } else {
      h[i] = 1;
      la[i] = i;
    }
  for (int i = 2; i <= n; i++)
    h[i] = (long long)h[i] * h[i - 1] % mod;
  memset(sum, 0, sizeof(sum));
  for (int i = 1; i <= n; i++) {
    ans1 = ((long long)(query(a[i]) - query(la[a[i]] - 1) + mod) * h[a[i]] +
            ans1) %
           mod;
    add(a[i], inv(h[a[i]]));
  }
  ans1 = (long long)ans1 * inv(2) % mod;
  memset(sum, 0, sizeof(sum));
  for (int i = 1; i <= n; i++) {
    (ans1 += i - 1 - query(a[i])) %= mod;
    add(a[i], 1);
  }
  memset(sum, 0, sizeof(sum));
  for (int i = n; i >= 1; i--) {
    ans2 = ((long long)(query(a[i] - 1) - query(la[a[i]] - 1) + mod) * h[a[i]] +
            ans2) %
           mod;
    add(a[i], inv(h[a[i]]));
  }
  ans2 = (long long)ans2 * inv(2) % mod;
  ans1 = (ans1 - ans2 + mod) % mod;
  printf("%lld", (long long)ans1 * pn % mod);
  return 0;
}