Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <vector>

typedef long long LL;
const int Mod = 1000000007;
const int MN = 100005;

inline int qPow(int b, int e) {
  int a = 1;
  for (; e; e >>= 1, b = (LL)b * b % Mod)
    if (e & 1)
      a = (LL)a * b % Mod;
  return a;
}

int Fac[MN], iFac[MN];
inline void Init(int N) {
  Fac[0] = 1;
  for (int i = 1; i <= N; ++i)
    Fac[i] = (LL)Fac[i - 1] * i % Mod;
  iFac[N] = qPow(Fac[N], Mod - 2);
  for (int i = N; i >= 1; --i)
    iFac[i - 1] = (LL)iFac[i] * i % Mod;
}

int N, X, Y, p1, p2, C;
std::vector<int> V[MN], G[MN];
int index[MN], col[MN];
inline void Ins(int x, int y) {
  G[x].push_back(y);
  G[y].push_back(x);
}

int vis[MN], buk[MN], stk[MN], tp;
void DFS(int u) {
  vis[u] = 1, ++buk[col[u]], stk[++tp] = u;
  for (int v : G[u])
    if (!vis[v])
      DFS(v);
}

int main() {
  scanf("%d%d%d", &N, &X, &Y);
  Init(N);
  for (int i = 1, x, y; i <= N; ++i) {
    scanf("%d%d", &x, &y);
    V[x].push_back(y);
  }
  for (int i = 1; i <= N; ++i)
    if (!V[i].empty()) {
      std::sort(V[i].begin(), V[i].end());
      if (!p1)
        p1 = i;
      else {
        if (!p2 || V[p2][0] > V[i][0])
          p2 = i;
        if (V[p1][0] > V[p2][0])
          std::swap(p1, p2);
      }
      int s = V[i].size();
      index[i] = C += s;
      for (int j = 0; j < s; ++j)
        col[C - j] = i;
    }
  for (int i = 1; i <= N; ++i)
    if (!V[i].empty()) {
      int s = V[i].size(), c = index[i];
      for (int j = 1; j < s && V[i][0] + V[i][j] <= X; ++j)
        Ins(c, c - j);
      if (p1 && i != p1)
        for (int j = 0; j < s && V[p1][0] + V[i][j] <= Y; ++j)
          Ins(index[p1], c - j);
      if (p2 && i == p1)
        for (int j = 1; j < s && V[p2][0] + V[i][j] <= Y; ++j)
          Ins(index[p2], c - j);
    }
  int Ans = 1;
  for (int i = 1; i <= N; ++i)
    if (!vis[i]) {
      DFS(i);
      int s = 0;
      for (int x = 0; tp; --tp)
        if (buk[x = col[stk[tp]]])
          s += buk[x], Ans = (LL)Ans * iFac[buk[x]] % Mod, buk[x] = 0;
      Ans = (LL)Ans * Fac[s] % Mod;
    }
  printf("%d\n", Ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>

typedef long long LL;
const int Mod = 1000000007;
const int MN = 200005;

inline int qPow(int b, int e) {
  int a = 1;
  for (; e; e >>= 1, b = (LL)b * b % Mod)
    if (e & 1)
      a = (LL)a * b % Mod;
  return a;
}

int Fac[MN], iFac[MN];
inline void Init(int N) {
  Fac[0] = 1;
  for (int i = 1; i <= N; ++i)
    Fac[i] = (LL)Fac[i - 1] * i % Mod;
  iFac[N] = qPow(Fac[N], Mod - 2);
  for (int i = N; i >= 1; --i)
    iFac[i - 1] = (LL)iFac[i] * i % Mod;
}

int N, X, Y, p1, p2, C;
std::vector<int> V[MN], G[MN];
int index[MN], col[MN];
inline void Ins(int x, int y) {
  G[x].push_back(y);
  G[y].push_back(x);
}

int vis[MN], buk[MN], stk[MN], tp;
void DFS(int u) {
  vis[u] = 1, ++buk[col[u]], stk[++tp] = u;
  for (int v : G[u])
    if (!vis[v])
      DFS(v);
}

int main() {
  scanf("%d%d%d", &N, &X, &Y);
  Init(N);
  for (int i = 1, x, y; i <= N; ++i) {
    scanf("%d%d", &x, &y);
    V[x].push_back(y);
  }
  for (int i = 1; i <= N; ++i)
    if (!V[i].empty()) {
      std::sort(V[i].begin(), V[i].end());
      if (!p1)
        p1 = i;
      else {
        if (!p2 || V[p2][0] > V[i][0])
          p2 = i;
        if (V[p1][0] > V[p2][0])
          std::swap(p1, p2);
      }
      int s = V[i].size();
      index[i] = C += s;
      for (int j = 0; j < s; ++j)
        col[C - j] = i;
    }
  for (int i = 1; i <= N; ++i)
    if (!V[i].empty()) {
      int s = V[i].size(), c = index[i];
      for (int j = 1; j < s && V[i][0] + V[i][j] <= X; ++j)
        Ins(c, c - j);
      if (p1 && i != p1)
        for (int j = 0; j < s && V[p1][0] + V[i][j] <= Y; ++j)
          Ins(index[p1], c - j);
      if (p2 && i == p1)
        for (int j = 1; j < s && V[p2][0] + V[i][j] <= Y; ++j)
          Ins(index[p2], c - j);
    }
  int Ans = 1;
  for (int i = 1; i <= N; ++i)
    if (!vis[i]) {
      DFS(i);
      int s = 0;
      for (int x = 0; tp; --tp)
        if (buk[x = col[stk[tp]]])
          s += buk[x], Ans = (LL)Ans * iFac[buk[x]] % Mod, buk[x] = 0;
      Ans = (LL)Ans * Fac[s] % Mod;
    }
  printf("%d\n", Ans);
  return 0;
}