Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define LL long long
#define maxn 200005
#define mod 1000000007
#define INF (LL)1e18
using namespace std;
typedef pair<LL, int> PLI;
LL n, ans, c[maxn], Siz, cnt[maxn], fact[maxn], inv[maxn], mnp[maxn], Mnp2, Mnp;
LL w[maxn], x, y, Mn = INF, Mn2 = INF, mn[maxn];
bool vis[maxn];
vector<PLI> cw[maxn];
struct node {
  LL v;
  node *nxt;
} edge[maxn * 75], *head[maxn], *ncnt;
void addedge(LL u, LL v) {
  ncnt++;
  ncnt->v = v, ncnt->nxt = head[u];
  head[u] = ncnt;
}
void dfs(LL u) {
  vis[u] = true;
  cnt[c[u]]++, Siz++;
  for (node *p = head[u]; p; p = p->nxt) {
    LL v = p->v;
    if (!vis[v])
      dfs(v);
  }
}
int main() {
  scanf("%lld%lld%lld", &n, &x, &y);
  fact[0] = 1;
  for (LL i = 1; i <= n; i++)
    fact[i] = 1ll * fact[i - 1] * i % mod;
  inv[1] = 1;
  for (LL i = 2; i <= n; i++)
    inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
  inv[0] = 1;
  for (LL i = 1; i <= n; i++)
    inv[i] = 1ll * inv[i] * inv[i - 1] % mod;
  ncnt = &edge[0];
  for (LL i = 1; i <= n; i++)
    mn[i] = INF;
  for (LL i = 1; i <= n; i++) {
    scanf("%lld%lld", &c[i], &w[i]);
    if (mn[c[i]] > w[i])
      mn[c[i]] = w[i], mnp[c[i]] = i;
    if (Mn > w[i])
      Mn = w[i], Mnp = i; // Mn=min(Mn,w[i]);
  }
  for (LL i = 1; i <= n; i++)
    if (c[i] != c[Mnp] && Mn2 > w[i])
      Mn2 = w[i], Mnp2 = i;
  for (LL i = 1; i <= n; i++)
    if (i != mnp[c[i]] && mn[c[i]] + w[i] <= x) {
      addedge(i, mnp[c[i]]);
      addedge(mnp[c[i]], i);
    }
  for (LL i = 1; i <= n; i++) {
    if (i == Mnp)
      continue;
    else if (c[i] != c[Mnp] && Mn + w[i] <= y) {
      addedge(i, Mnp);
      addedge(Mnp, i);
    } else if (Mn2 != INF && c[i] != c[Mn2] && Mn2 + w[i] <= y) {
      addedge(i, Mnp2);
      addedge(Mnp2, i);
    }
  }
  dfs(Mnp);
  ans = fact[Siz];
  for (LL i = 1; i <= n; i++)
    ans = 1ll * ans * inv[cnt[i]] % mod;
  printf("%lld\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define LL long long
#define maxn 200005
#define mod 1000000007
#define INF (LL)1e18
using namespace std;
typedef pair<LL, int> PLI;
LL n, ans, c[maxn], Siz, cnt[maxn], fact[maxn], inv[maxn], mnp[maxn], Mnp2, Mnp;
LL w[maxn], x, y, Mn = INF, Mn2 = INF, mn[maxn];
bool vis[maxn];
vector<PLI> cw[maxn];
struct node {
  LL v;
  node *nxt;
} edge[maxn * 75], *head[maxn], *ncnt;
void addedge(LL u, LL v) {
  ncnt++;
  ncnt->v = v, ncnt->nxt = head[u];
  head[u] = ncnt;
}
void dfs(LL u) {
  vis[u] = true;
  cnt[c[u]]++, Siz++;
  for (node *p = head[u]; p; p = p->nxt) {
    LL v = p->v;
    if (!vis[v])
      dfs(v);
  }
}
int main() {
  scanf("%lld%lld%lld", &n, &x, &y);
  fact[0] = 1;
  for (LL i = 1; i <= n; i++)
    fact[i] = 1ll * fact[i - 1] * i % mod;
  inv[1] = 1;
  for (LL i = 2; i <= n; i++)
    inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
  inv[0] = 1;
  for (LL i = 1; i <= n; i++)
    inv[i] = 1ll * inv[i] * inv[i - 1] % mod;
  ncnt = &edge[0];
  for (LL i = 1; i <= n; i++)
    mn[i] = INF;
  for (LL i = 1; i <= n; i++) {
    scanf("%lld%lld", &c[i], &w[i]);
    if (mn[c[i]] > w[i])
      mn[c[i]] = w[i], mnp[c[i]] = i;
    if (Mn > w[i])
      Mn = w[i], Mnp = i; // Mn=min(Mn,w[i]);
  }
  for (LL i = 1; i <= n; i++)
    if (c[i] != c[Mnp] && Mn2 > w[i])
      Mn2 = w[i], Mnp2 = i;
  for (LL i = 1; i <= n; i++)
    if (i != mnp[c[i]] && mn[c[i]] + w[i] <= x) {
      addedge(i, mnp[c[i]]);
      addedge(mnp[c[i]], i);
    }
  for (LL i = 1; i <= n; i++) {
    if (i == Mnp)
      continue;
    else if (c[i] != c[Mnp] && Mn + w[i] <= y) {
      addedge(i, Mnp);
      addedge(Mnp, i);
    } else if (c[i] == c[Mnp] && Mn2 + w[i] <= y) {
      addedge(i, Mnp2);
      addedge(Mnp2, i);
    }
  }
  dfs(Mnp);
  ans = fact[Siz];
  for (LL i = 1; i <= n; i++)
    ans = 1ll * ans * inv[cnt[i]] % mod;
  printf("%lld\n", ans);
}