Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007ll
using namespace std;
struct edge {
  int to;
  int nxt;
} e[3000010];
int color[200010], head[200010], pos[200010];
int cnt[200010], tot;
long long w[200010], minn[200010];
long long fac[200010], rfac[200010];
int v[200010];
int n, minnum, minnum2;
long long X, Y, min1, min2, ans;
long long qpow(long long a, long long b) {
  long long ans = 1, res = a;
  while (b) {
    if (b & 1)
      ans = (ans * res) % mod;
    res *= res;
    res %= mod;
    b >>= 1;
  }
  return ans;
}
long long C(long long n, long long k) {
  return fac[n] * rfac[k] % mod * rfac[n - k] % mod;
}
void addedge(int x, int y) {
  e[++tot].nxt = head[x];
  e[tot].to = y;
  head[x] = tot;
}
void add(int x, int y) {
  addedge(x, y);
  addedge(y, x);
}
void dfs(int x) {
  v[x] = 1;
  for (int i = head[x]; i; i = e[i].nxt) {
    if (!v[e[i].to])
      dfs(e[i].to);
  }
}
int main() {
  scanf("%d%lld%lld", &n, &X, &Y);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    scanf("%d%lld", color[i], w[i]);
  for (int i = 1; i <= n; i++)
    fac[i] = (i * fac[i - 1]) % mod, minn[i] = 2147483647ll;
  rfac[n] = qpow(fac[n], mod - 2);
  for (int i = n; i > 0; i--)
    rfac[i - 1] = (i * rfac[i]) % mod;
  for (int i = 1; i <= n; i++) {
    if (w[i] < minn[color[i]]) {
      minn[color[i]] = w[i];
      pos[color[i]] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i != pos[color[i]] && w[i] + minn[color[i]] <= X) {
      add(i, pos[color[i]]);
    }
  }
  min1 = min2 = 2147483647ll;
  for (int i = 1; i <= n; i++) {
    if (w[i] < min1) {
      min1 = w[i];
      minnum = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (w[i] <= min2 && color[i] != color[minnum]) {
      min2 = w[i];
      minnum2 = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == minnum)
      continue;
    if (color[i] != color[minnum] && w[i] + w[minnum] <= Y) {
      add(i, minnum);
    }
    if (minnum2 && color[i] != color[minnum2] && w[i] + w[minnum2] <= Y) {
      add(i, minnum2);
    }
  }
  dfs(minnum);
  long long tot = 0;
  for (int i = 1; i <= n; i++) {
    if (v[i]) {
      ++tot;
      cnt[color[i]]++;
    }
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    if (cnt[i]) {
      ans = (ans * C(tot, cnt[i])) % mod;
      tot -= cnt[i];
    }
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007ll
using namespace std;
struct edge {
  int to;
  int nxt;
} e[3000010];
int color[200010], head[200010], pos[200010];
int cnt[200010], tot;
long long w[200010], minn[200010];
long long fac[200010], rfac[200010];
int v[200010];
int n, minnum, minnum2;
long long X, Y, min1, min2, ans;
long long qpow(long long a, long long b) {
  long long ans = 1, res = a;
  while (b) {
    if (b & 1)
      ans = (ans * res) % mod;
    res *= res;
    res %= mod;
    b >>= 1;
  }
  return ans;
}
long long C(long long n, long long k) {
  return fac[n] * rfac[k] % mod * rfac[n - k] % mod;
}
void addedge(int x, int y) {
  e[++tot].nxt = head[x];
  e[tot].to = y;
  head[x] = tot;
}
void add(int x, int y) {
  addedge(x, y);
  addedge(y, x);
}
void dfs(int x) {
  v[x] = 1;
  for (int i = head[x]; i; i = e[i].nxt) {
    if (!v[e[i].to])
      dfs(e[i].to);
  }
}
int main() {
  scanf("%d%lld%lld", &n, &X, &Y);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    scanf("%d%lld", &color[i], &w[i]);
  for (int i = 1; i <= n; i++)
    fac[i] = (i * fac[i - 1]) % mod, minn[i] = 2147483647ll;
  rfac[n] = qpow(fac[n], mod - 2);
  for (int i = n; i > 0; i--)
    rfac[i - 1] = (i * rfac[i]) % mod;
  for (int i = 1; i <= n; i++) {
    if (w[i] < minn[color[i]]) {
      minn[color[i]] = w[i];
      pos[color[i]] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i != pos[color[i]] && w[i] + minn[color[i]] <= X) {
      add(i, pos[color[i]]);
    }
  }
  min1 = min2 = 2147483647ll;
  for (int i = 1; i <= n; i++) {
    if (w[i] < min1) {
      min1 = w[i];
      minnum = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (w[i] <= min2 && color[i] != color[minnum]) {
      min2 = w[i];
      minnum2 = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == minnum)
      continue;
    if (color[i] != color[minnum] && w[i] + w[minnum] <= Y) {
      add(i, minnum);
    }
    if (minnum2 && color[i] != color[minnum2] && w[i] + w[minnum2] <= Y) {
      add(i, minnum2);
    }
  }
  dfs(minnum);
  long long tot = 0;
  for (int i = 1; i <= n; i++) {
    if (v[i]) {
      ++tot;
      cnt[color[i]]++;
    }
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    if (cnt[i]) {
      ans = (ans * C(tot, cnt[i])) % mod;
      tot -= cnt[i];
    }
  }
  printf("%lld", ans);
  return 0;
}