Problem p03367 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = 1000000007;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

class range {
private:
  struct I {
    int x;
    int operator*() { return x; }
    bool operator!=(I &lhs) { return x < lhs.x; }
    void operator++() { ++x; }
  };
  I i, n;

public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  I &begin() { return i; }
  I &end() { return n; }
};

// head

const int N = 101000;
int n, vis[N];
PII p[N];
ll ret, prec[N], sufc[N], c[N];

struct node {
  ll fgp, fgv;
  ll sp, sv;
} nd[4 * N];
void upd(int p) {
  nd[p].sp = (nd[p + p].sp + nd[p + p + 1].sp) % mod;
  nd[p].sv = (nd[p + p].sv + nd[p + p + 1].sv) % mod;
}
void setf(int p, ll fgp, ll fgv) {
  nd[p].fgv = (nd[p].fgv + fgv * nd[p].fgp) % mod;
  nd[p].fgp = nd[p].fgp * fgp % mod;
  nd[p].sv = (nd[p].sv + fgv * nd[p].sp) % mod;
  nd[p].sp = nd[p].sp * fgp % mod;
}
void build(int p, int l, int r) {
  nd[p].fgp = 1;
  if (l == r) {
  } else {
    int md = (l + r) >> 1;
    build(p + p, l, md);
    build(p + p + 1, md + 1, r);
    upd(p);
  }
}
void push(int p) {
  if (nd[p].fgp != 1 || nd[p].fgv != 0) {
    setf(p + p, nd[p].fgp, nd[p].fgv);
    setf(p + p + 1, nd[p].fgp, nd[p].fgv);
    nd[p].fgp = 1;
    nd[p].fgv = 0;
  }
}
ll query(int p, int l, int r, int tl, int tr) {
  if (tl == l && tr == r)
    return nd[p].sv;
  else {
    push(p);
    int md = (l + r) >> 1;
    if (tr <= md)
      return query(p + p, l, md, tl, tr);
    else if (tl > md)
      return query(p + p + 1, md + 1, r, tl, tr);
    else
      return (query(p + p, l, md, tl, md) +
              query(p + p + 1, md + 1, r, md + 1, tr)) %
             mod;
  }
}
void modify(int p, int l, int r, int x, ll v) {
  if (l == r) {
    nd[p].sp = v;
  } else {
    push(p);
    int md = (l + r) >> 1;
    if (x <= md)
      modify(p + p, l, md, x, v);
    else if (x > md)
      modify(p + p + 1, md + 1, r, x, v);
    upd(p);
  }
}

int main() {
  scanf("%d", &n);
  rep(i, 1, n + 1) scanf("%d", &p[i].fi), p[i].se = i;
  sort(p + 1, p + n + 1);
  rep(i, 1, n + 1) c[i] = p[i].fi - i + 1;
  prec[0] = 1;
  sufc[n] = 1;
  rep(i, 1, n + 1) prec[i] = prec[i - 1] * c[i] % mod;
  per(i, 1, n + 1) sufc[i - 1] = sufc[i] * c[i] % mod;
  ll totval = 0;
  build(1, 1, n);
  per(i, 1, n + 1) {
    totval = (totval * (c[i + 1] - 1) + sufc[i + 1]) % mod;
    ret = (ret + totval * prec[i - 1] % mod * (c[i] * (c[i] - 1) / 2 % mod)) %
          mod;
    ll key = prec[i] * (p[i + 1].fi - p[i].fi) % mod;

    setf(1, c[i + 1] - 1, 0);
    if (i < n)
      modify(1, 1, n, p[i + 1].se, sufc[i + 1]);
    setf(1, 1, key);
    if (p[i].se > 1)
      ret = (ret + query(1, 1, n, 1, p[i].se - 1)) % mod;
  }
  printf("%lld\n", ret);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = 1000000007;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

class range {
private:
  struct I {
    int x;
    int operator*() { return x; }
    bool operator!=(I &lhs) { return x < lhs.x; }
    void operator++() { ++x; }
  };
  I i, n;

public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  I &begin() { return i; }
  I &end() { return n; }
};

// head

const int N = 401000;
int n, vis[N];
PII p[N];
ll ret, prec[N], sufc[N], c[N];

struct node {
  ll fgp, fgv;
  ll sp, sv;
} nd[4 * N];
void upd(int p) {
  nd[p].sp = (nd[p + p].sp + nd[p + p + 1].sp) % mod;
  nd[p].sv = (nd[p + p].sv + nd[p + p + 1].sv) % mod;
}
void setf(int p, ll fgp, ll fgv) {
  nd[p].fgv = (nd[p].fgv + fgv * nd[p].fgp) % mod;
  nd[p].fgp = nd[p].fgp * fgp % mod;
  nd[p].sv = (nd[p].sv + fgv * nd[p].sp) % mod;
  nd[p].sp = nd[p].sp * fgp % mod;
}
void build(int p, int l, int r) {
  nd[p].fgp = 1;
  if (l == r) {
  } else {
    int md = (l + r) >> 1;
    build(p + p, l, md);
    build(p + p + 1, md + 1, r);
    upd(p);
  }
}
void push(int p) {
  if (nd[p].fgp != 1 || nd[p].fgv != 0) {
    setf(p + p, nd[p].fgp, nd[p].fgv);
    setf(p + p + 1, nd[p].fgp, nd[p].fgv);
    nd[p].fgp = 1;
    nd[p].fgv = 0;
  }
}
ll query(int p, int l, int r, int tl, int tr) {
  if (tl == l && tr == r)
    return nd[p].sv;
  else {
    push(p);
    int md = (l + r) >> 1;
    if (tr <= md)
      return query(p + p, l, md, tl, tr);
    else if (tl > md)
      return query(p + p + 1, md + 1, r, tl, tr);
    else
      return (query(p + p, l, md, tl, md) +
              query(p + p + 1, md + 1, r, md + 1, tr)) %
             mod;
  }
}
void modify(int p, int l, int r, int x, ll v) {
  if (l == r) {
    nd[p].sp = v;
  } else {
    push(p);
    int md = (l + r) >> 1;
    if (x <= md)
      modify(p + p, l, md, x, v);
    else if (x > md)
      modify(p + p + 1, md + 1, r, x, v);
    upd(p);
  }
}

int main() {
  scanf("%d", &n);
  rep(i, 1, n + 1) scanf("%d", &p[i].fi), p[i].se = i;
  sort(p + 1, p + n + 1);
  rep(i, 1, n + 1) c[i] = p[i].fi - i + 1;
  prec[0] = 1;
  sufc[n] = 1;
  rep(i, 1, n + 1) prec[i] = prec[i - 1] * c[i] % mod;
  per(i, 1, n + 1) sufc[i - 1] = sufc[i] * c[i] % mod;
  ll totval = 0;
  build(1, 1, n);
  per(i, 1, n + 1) {
    totval = (totval * (c[i + 1] - 1) + sufc[i + 1]) % mod;
    ret = (ret + totval * prec[i - 1] % mod * (c[i] * (c[i] - 1) / 2 % mod)) %
          mod;
    ll key = prec[i] * (p[i + 1].fi - p[i].fi) % mod;

    setf(1, c[i + 1] - 1, 0);
    if (i < n)
      modify(1, 1, n, p[i + 1].se, sufc[i + 1]);
    setf(1, 1, key);
    if (p[i].se > 1)
      ret = (ret + query(1, 1, n, 1, p[i].se - 1)) % mod;
  }
  printf("%lld\n", ret);
}