Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mo 1000000007
using namespace std;
long long n, i, j, jc[200005], ny[200005], book[200005];
long long ans, sum, flag[200005], flagg;
long long x, y, top, nex[600005], fir[200005], to[600005];
struct nyh {
  long long c, w, id;
} o[200005];
long long read() {
  char ch = getchar();
  long long xx = 0;
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx;
}
bool cmp(nyh u, nyh v) { return u.w < v.w; }
bool cmp2(nyh u, nyh v) { return u.c < v.c || u.c == v.c && u.w < v.w; }
bool cmp3(nyh u, nyh v) { return u.id < v.id; }
int lj(long long u, long long v) {
  top++;
  to[top] = v;
  nex[top] = fir[u];
  fir[u] = top;
}
long long ksm(long long u, long long v) {
  long long o = 1;
  while (v) {
    if (v & 1)
      o = o * u % mo;
    u = u * u % mo;
    v >>= 1;
  }
  return o;
}
long long C(long long u, long long v) {
  return jc[u] * ny[v] % mo * ny[u - v] % mo;
}
long long ss(long long v) {
  book[v] = 1;
  ans = ans * ksm(C(sum, flag[o[v].c]), mo - 2) % mo;
  sum++;
  flag[o[v].c]++;
  ans = ans * C(sum, flag[o[v].c]) % mo;
  for (int top1 = fir[v]; top1; top1 = nex[top1])
    if (book[to[top1]] == 0)
      ss(to[top1]);
}
long long ss2(long long v) {
  book[v] = 2;
  flag[o[v].c] = 0;
  for (int top1 = fir[v]; top1; top1 = nex[top1])
    if (book[to[top1]] != 2)
      ss2(to[top1]);
}
int main() {
  n = read();
  x = read();
  y = read();
  for (i = 1; i <= n; i++) {
    o[i].c = read();
    o[i].w = read();
    o[i].id = i;
  }
  sort(o + 1, o + n + 1, cmp);
  for (i = n; i >= 2; i--)
    if (o[1].c != o[i].c && o[1].w + o[i].w <= y) {
      flagg = i;
      lj(o[1].id, o[i].id);
      lj(o[i].id, o[1].id);
    }
  if (flagg != 0)
    for (i = 1; i <= n; i++)
      if (o[i].c != o[flagg].c && o[i].w + o[flagg].w <= y) {
        lj(o[i].id, o[flagg].id);
        lj(o[flagg].id, o[i].id);
      }
  sort(o + 1, o + n + 1, cmp2);
  for (i = 1; i <= n; i++)
    if (i == 1 || o[i].c != o[i - 1].c) {
      for (j = i + 1; o[j].c == o[i].c; j++)
        if (o[i].w + o[j].w <= x) {
          lj(o[i].id, o[j].id);
          lj(o[j].id, o[i].id);
        }
    }
  jc[0] = 1;
  ny[0] = 1;
  for (i = 1; i <= n; i++) {
    jc[i] = jc[i - 1] * i % mo;
    ny[i] = ksm(jc[i], mo - 2);
  }
  sort(o + 1, o + n + 1, cmp3);
  ans = 1;
  for (i = 1; i <= n; i++)
    if (book[i] == 0) {
      sum = 0;
      ss(i);
      ss2(i);
    }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mo 1000000007
using namespace std;
long long n, i, j, jc[200005], ny[200005], book[200005];
long long ans, sum, flag[200005], flagg;
long long x, y, top, nex[1200005], fir[200005], to[1200005];
struct nyh {
  long long c, w, id;
} o[200005];
long long read() {
  char ch = getchar();
  long long xx = 0;
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx;
}
bool cmp(nyh u, nyh v) { return u.w < v.w; }
bool cmp2(nyh u, nyh v) { return u.c < v.c || u.c == v.c && u.w < v.w; }
bool cmp3(nyh u, nyh v) { return u.id < v.id; }
int lj(long long u, long long v) {
  top++;
  to[top] = v;
  nex[top] = fir[u];
  fir[u] = top;
}
long long ksm(long long u, long long v) {
  long long o = 1;
  while (v) {
    if (v & 1)
      o = o * u % mo;
    u = u * u % mo;
    v >>= 1;
  }
  return o;
}
long long C(long long u, long long v) {
  return jc[u] * ny[v] % mo * ny[u - v] % mo;
}
long long ss(long long v) {
  book[v] = 1;
  ans = ans * ksm(C(sum, flag[o[v].c]), mo - 2) % mo;
  sum++;
  flag[o[v].c]++;
  ans = ans * C(sum, flag[o[v].c]) % mo;
  for (int top1 = fir[v]; top1; top1 = nex[top1])
    if (book[to[top1]] == 0)
      ss(to[top1]);
}
long long ss2(long long v) {
  book[v] = 2;
  flag[o[v].c] = 0;
  for (int top1 = fir[v]; top1; top1 = nex[top1])
    if (book[to[top1]] != 2)
      ss2(to[top1]);
}
int main() {
  n = read();
  x = read();
  y = read();
  for (i = 1; i <= n; i++) {
    o[i].c = read();
    o[i].w = read();
    o[i].id = i;
  }
  sort(o + 1, o + n + 1, cmp);
  for (i = n; i >= 2; i--)
    if (o[1].c != o[i].c && o[1].w + o[i].w <= y) {
      flagg = i;
      lj(o[1].id, o[i].id);
      lj(o[i].id, o[1].id);
    }
  if (flagg != 0)
    for (i = 1; i <= n; i++)
      if (o[i].c != o[flagg].c && o[i].w + o[flagg].w <= y) {
        lj(o[i].id, o[flagg].id);
        lj(o[flagg].id, o[i].id);
      }
  sort(o + 1, o + n + 1, cmp2);
  for (i = 1; i <= n; i++)
    if (i == 1 || o[i].c != o[i - 1].c) {
      for (j = i + 1; o[j].c == o[i].c; j++)
        if (o[i].w + o[j].w <= x) {
          lj(o[i].id, o[j].id);
          lj(o[j].id, o[i].id);
        }
    }
  jc[0] = 1;
  ny[0] = 1;
  for (i = 1; i <= n; i++) {
    jc[i] = jc[i - 1] * i % mo;
    ny[i] = ksm(jc[i], mo - 2);
  }
  sort(o + 1, o + n + 1, cmp3);
  ans = 1;
  for (i = 1; i <= n; i++)
    if (book[i] == 0) {
      sum = 0;
      ss(i);
      ss2(i);
    }
  printf("%lld\n", ans);
  return 0;
}