Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x)                                                                  \
  {                                                                            \
    freopen(#x ".in", "r", stdin);                                             \
    freopen(#x ".out", "w", stdout);                                           \
  }
#define Edg                                                                    \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ];                                         \
  void ad_de(int a, int b) {                                                   \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
  }                                                                            \
  void adde(int a, int b) {                                                    \
    ad_de(a, b);                                                               \
    ad_de(b, a);                                                               \
  }
#define Edgc                                                                   \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ], vc[SZ];                                 \
  void ad_de(int a, int b, int c) {                                            \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
    vc[M] = c;                                                                 \
  }                                                                            \
  void adde(int a, int b, int c) {                                             \
    ad_de(a, b, c);                                                            \
    ad_de(b, a, c);                                                            \
  }
#define es(x, e) (int e = fst[x]; e; e = nxt[e])
#define esb(x, e, b) (int e = fst[x], b = vb[e]; e; e = nxt[e], b = vb[e])
#define SZ 666666
int n, X, Y, c[SZ], w[SZ];
pii ps[SZ];
vector<int> vs[SZ];
Edg bool vis[SZ];
map<int, int> sz;
void dfs(int x) {
  vis[x] = 1;
  ++sz[c[x]];
        for
          esb(x, e, b) if (!vis[b]) dfs(b);
}
const int MOD = 1e9 + 7;
ll fac[SZ], rfac[SZ];
ll qp(ll a, ll b) {
        ll x = 1;
        a %= MOD;
        while (b) {
          if (b & 1)
            x = x * a % MOD;
          a = a * a % MOD;
          b >>= 1;
        }
        return x;
}
ll C(int a, int b) { return fac[a] * rfac[a - b] % MOD * rfac[b] % MOD; }
int main() {
        fac[0] = rfac[0] = 1;
        for (int i = 1; i < SZ; ++i)
          fac[i] = fac[i - 1] * i % MOD;
        rfac[SZ - 1] = qp(fac[SZ - 1], MOD - 2);
        for (int i = SZ - 1; i >= 1; --i)
          rfac[i - 1] = rfac[i] * i % MOD;
        memset(ps, 120, sizeof ps);
        scanf("%d%d%d", &n, &X, &Y);
        for (int i = 1; i <= n; ++i) {
          scanf("%d%d", c + i, w + i);
          vs[c[i]].pb(i);
          ps[c[i]] = min(ps[c[i]], pii(w[i], i));
        }
        for (int i = 1; i <= n; ++i) {
          int x = ps[i].se, y = ps[i].fi;
          for (auto g : vs[i])
            if (y + w[g] <= X)
              adde(g, x);
        }
        sort(ps + 1, ps + 1 + n);
        int a = ps[1].se, b = ps[2].se;
        for (int i = 1; i <= n; ++i) {
          if (1 <= a && a <= n) {
            if (c[i] != c[a] && w[i] + w[a] <= Y)
              adde(i, a);
          }
          if (1 <= b && b <= n) {
            if (c[i] != c[b] && w[i] + w[b] <= Y)
              adde(i, b);
          }
        }
        ll cp = 1;
        for (int i = 1; i <= n; ++i)
          if (!vis[i]) {
            sz.clear();
            dfs(i);
            int s = 0;
            for (auto g : sz) {
              int p = g.se;
              cp = cp * C(s += p, p) % MOD;
            }
          }
        printf("%d\n", int(cp));
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x)                                                                  \
  {                                                                            \
    freopen(#x ".in", "r", stdin);                                             \
    freopen(#x ".out", "w", stdout);                                           \
  }
#define Edg                                                                    \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ];                                         \
  void ad_de(int a, int b) {                                                   \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
  }                                                                            \
  void adde(int a, int b) {                                                    \
    ad_de(a, b);                                                               \
    ad_de(b, a);                                                               \
  }
#define Edgc                                                                   \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ], vc[SZ];                                 \
  void ad_de(int a, int b, int c) {                                            \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
    vc[M] = c;                                                                 \
  }                                                                            \
  void adde(int a, int b, int c) {                                             \
    ad_de(a, b, c);                                                            \
    ad_de(b, a, c);                                                            \
  }
#define es(x, e) (int e = fst[x]; e; e = nxt[e])
#define esb(x, e, b) (int e = fst[x], b = vb[e]; e; e = nxt[e], b = vb[e])
#define SZ 1234567
int n, X, Y, c[SZ], w[SZ];
pii ps[SZ];
vector<int> vs[SZ];
Edg bool vis[SZ];
map<int, int> sz;
void dfs(int x) {
  vis[x] = 1;
  ++sz[c[x]];
        for
          esb(x, e, b) if (!vis[b]) dfs(b);
}
const int MOD = 1e9 + 7;
ll fac[SZ], rfac[SZ];
ll qp(ll a, ll b) {
        ll x = 1;
        a %= MOD;
        while (b) {
          if (b & 1)
            x = x * a % MOD;
          a = a * a % MOD;
          b >>= 1;
        }
        return x;
}
ll C(int a, int b) { return fac[a] * rfac[a - b] % MOD * rfac[b] % MOD; }
int main() {
        fac[0] = rfac[0] = 1;
        for (int i = 1; i < SZ; ++i)
          fac[i] = fac[i - 1] * i % MOD;
        rfac[SZ - 1] = qp(fac[SZ - 1], MOD - 2);
        for (int i = SZ - 1; i >= 1; --i)
          rfac[i - 1] = rfac[i] * i % MOD;
        memset(ps, 120, sizeof ps);
        scanf("%d%d%d", &n, &X, &Y);
        for (int i = 1; i <= n; ++i) {
          scanf("%d%d", c + i, w + i);
          vs[c[i]].pb(i);
          ps[c[i]] = min(ps[c[i]], pii(w[i], i));
        }
        for (int i = 1; i <= n; ++i) {
          int x = ps[i].se, y = ps[i].fi;
          for (auto g : vs[i])
            if (y + w[g] <= X)
              adde(g, x);
        }
        sort(ps + 1, ps + 1 + n);
        int a = ps[1].se, b = ps[2].se;
        for (int i = 1; i <= n; ++i) {
          if (1 <= a && a <= n) {
            if (c[i] != c[a] && w[i] + w[a] <= Y)
              adde(i, a);
          }
          if (1 <= b && b <= n) {
            if (c[i] != c[b] && w[i] + w[b] <= Y)
              adde(i, b);
          }
        }
        ll cp = 1;
        for (int i = 1; i <= n; ++i)
          if (!vis[i]) {
            sz.clear();
            dfs(i);
            int s = 0;
            for (auto g : sz) {
              int p = g.se;
              cp = cp * C(s += p, p) % MOD;
            }
          }
        printf("%d\n", int(cp));
}