Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define REACH cerr << "reached line " << __LINE__ << endl
#define DBG(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl

using pi = pair<int, int>;
using vi = vector<int>;
using ld = long double;

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 << "[";
  REP(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "]";
  return os;
}

int read() {
  int i;
  scanf("%lld", &i);
  return i;
}

void printSpace() { printf(" "); }

void printEoln() { printf("\n"); }

void print(int x, int suc = 1) {
  printf("%lld", x);
  if (suc == 1)
    printEoln();
  if (suc == 2)
    printSpace();
}

string readString() {
  static char buf[3341919];
  scanf("%s", buf);
  return string(buf);
}

char *readCharArray() {
  static char buf[3341919];
  static int bufUsed = 0;
  char *ret = buf + bufUsed;
  scanf("%s", ret);
  bufUsed += strlen(ret) + 1;
  return ret;
}

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}

template <class T, class U> void chmin(T &a, U b) {
  if (a > b)
    a = b;
}

template <class T> T Sq(const T &t) { return t * t; }

const int inf = LLONG_MAX / 3;

const int mod = 1000000007;
template <class T, class U> void add(T &a, U b) { a = ((ll)a + b) % mod; }

template <class T, class U> void sub(T &a, U b) {
  a = ((ll)a - b % mod + mod) % mod;
}

template <class T, class U> void mult(T &a, U b) { a = ((ll)a * b) % mod; }

ll modPow(ll a, ll p) {
  ll s = 1;
  REP(i, 30) {
    if ((p >> i) & 1)
      mult(s, a);
    mult(a, a);
  }
  return s;
}

ll modInv(ll a) { return modPow(a, mod - 2); }

const int Nmax = 114514;
vi g[Nmax];
void AddEdge(int a, int b) {
  g[a].PB(b);
  g[b].PB(a);
}
bool vis[Nmax];
void dfs(int v, vi &z, const vi &c) {
  if (vis[v])
    return;
  vis[v] = 1;
  z.PB(c[v]);
  for (auto to : g[v])
    dfs(to, z, c);
}
pi s[Nmax], t[Nmax];

signed main() {
  int n = read(), x = read(), y = read();
  vi c, w;
  REP(i, n) c.PB(read() - 1), w.PB(read());
  REP(i, n) s[i] = pi(inf, inf);
  REP(i, n) chmin(s[c[i]], pi(w[i], i));
  REP(i, n) if (s[c[i]].first + w[i] <= x) AddEdge(i, s[c[i]].second);
  REP(i, n) t[i] = s[i];
  FOR(i, 1, n) chmin(s[i], s[i - 1]);
  for (int i = n - 2; i >= 0; i--)
    chmin(t[i], t[i + 1]);
  REP(i, n) {
    pi p = pi(inf, inf);
    if (c[i] - 1 >= 0)
      chmin(p, s[c[i] - 1]);
    if (c[i] + 1 < n)
      chmin(p, t[c[i] + 1]);
    if (p.first + w[i] <= y)
      AddEdge(i, p.second);
  }
  int ans = 1;
  REP(v, n) if (!vis[v]) {
    vi z;
    dfs(v, z, c);
    sort(ALL(z));
    FOR(i, 1, z.size() + 1) mult(ans, i);
    int len = 0;
    REP(i, z.size()) {
      if (i && z[i - 1] == z[i])
        len++;
      else
        len = 1;
      mult(ans, modInv(len));
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define REACH cerr << "reached line " << __LINE__ << endl
#define DBG(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl

using pi = pair<int, int>;
using vi = vector<int>;
using ld = long double;

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 << "[";
  REP(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "]";
  return os;
}

int read() {
  int i;
  scanf("%lld", &i);
  return i;
}

void printSpace() { printf(" "); }

void printEoln() { printf("\n"); }

void print(int x, int suc = 1) {
  printf("%lld", x);
  if (suc == 1)
    printEoln();
  if (suc == 2)
    printSpace();
}

string readString() {
  static char buf[3341919];
  scanf("%s", buf);
  return string(buf);
}

char *readCharArray() {
  static char buf[3341919];
  static int bufUsed = 0;
  char *ret = buf + bufUsed;
  scanf("%s", ret);
  bufUsed += strlen(ret) + 1;
  return ret;
}

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}

template <class T, class U> void chmin(T &a, U b) {
  if (a > b)
    a = b;
}

template <class T> T Sq(const T &t) { return t * t; }

const int inf = LLONG_MAX / 3;

const int mod = 1000000007;
template <class T, class U> void add(T &a, U b) { a = ((ll)a + b) % mod; }

template <class T, class U> void sub(T &a, U b) {
  a = ((ll)a - b % mod + mod) % mod;
}

template <class T, class U> void mult(T &a, U b) { a = ((ll)a * b) % mod; }

ll modPow(ll a, ll p) {
  ll s = 1;
  REP(i, 30) {
    if ((p >> i) & 1)
      mult(s, a);
    mult(a, a);
  }
  return s;
}

ll modInv(ll a) { return modPow(a, mod - 2); }

const int Nmax = 214514;
vi g[Nmax];
void AddEdge(int a, int b) {
  g[a].PB(b);
  g[b].PB(a);
}
bool vis[Nmax];
void dfs(int v, vi &z, const vi &c) {
  if (vis[v])
    return;
  vis[v] = 1;
  z.PB(c[v]);
  for (auto to : g[v])
    dfs(to, z, c);
}
pi s[Nmax], t[Nmax];

signed main() {
  int n = read(), x = read(), y = read();
  vi c, w;
  REP(i, n) c.PB(read() - 1), w.PB(read());
  REP(i, n) s[i] = pi(inf, inf);
  REP(i, n) chmin(s[c[i]], pi(w[i], i));
  REP(i, n) if (s[c[i]].first + w[i] <= x) AddEdge(i, s[c[i]].second);
  REP(i, n) t[i] = s[i];
  FOR(i, 1, n) chmin(s[i], s[i - 1]);
  for (int i = n - 2; i >= 0; i--)
    chmin(t[i], t[i + 1]);
  REP(i, n) {
    pi p = pi(inf, inf);
    if (c[i] - 1 >= 0)
      chmin(p, s[c[i] - 1]);
    if (c[i] + 1 < n)
      chmin(p, t[c[i] + 1]);
    if (p.first + w[i] <= y)
      AddEdge(i, p.second);
  }
  int ans = 1;
  REP(v, n) if (!vis[v]) {
    vi z;
    dfs(v, z, c);
    sort(ALL(z));
    FOR(i, 1, z.size() + 1) mult(ans, i);
    int len = 0;
    REP(i, z.size()) {
      if (i && z[i - 1] == z[i])
        len++;
      else
        len = 1;
      mult(ans, modInv(len));
    }
  }
  cout << ans << endl;
}