Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define ljc 1000000007
using namespace std;
#define gc getchar
inline ll read() {
  register ll x = 0, f = 1;
  char ch = gc();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = gc();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = gc();
  }
  return (f == 1) ? x : -x;
}
vector<pair<int, int>> B[101010];
vector<int> clr, e[101010];
int n, col[101010], w[101010], X, Y, cnt[101010], fac[101010], inv[101010],
    _inv[101010];
bool vis[101010];
void dfs(int u) {
  vis[u] = 1;
  cnt[col[u]]++;
  clr.push_back(col[u]);
  for (auto v : e[u]) {
    if (!vis[v])
      dfs(v);
  }
}
signed main() {
  n = read(), X = read(), Y = read();
  fac[0] = inv[0] = inv[1] = _inv[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % ljc;
  for (int i = 2; i <= n; i++)
    inv[i] = (ljc - (ljc / i) * inv[ljc % i] % ljc) % ljc;
  for (int i = 1; i <= n; i++)
    _inv[i] = _inv[i - 1] * inv[i] % ljc;
  for (int i = 1; i <= n; i++) {
    col[i] = read(), w[i] = read();
    B[col[i]].push_back(make_pair(w[i], i));
  }
  for (int i = 1; i <= n; i++) {
    if (B[i].size())
      sort(B[i].begin(), B[i].end());
    for (int j = 1; j < (int)B[i].size(); j++) {
      if (B[i][j].first + B[i][0].first <= X) {
        e[B[i][j].second].push_back(B[i][0].second);
        e[B[i][0].second].push_back(B[i][j].second);
      }
    }
  }
  int minid = 0, min2id = 0;
  B[0].push_back(make_pair(1000000001, 0));
  for (int i = 1; i <= n; i++) {
    if (B[i].size()) {
      if (B[i].front().first < B[minid].front().first) {
        min2id = minid, minid = i;
      } else if (B[i].front().first < B[min2id].front().first) {
        min2id = i;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == minid) {
      for (auto j : B[i]) {
        if (j.first + B[min2id].front().first <= Y) {
          e[j.second].push_back(B[min2id][0].second);
          e[B[min2id][0].second].push_back(j.second);
        }
      }
    } else {
      for (auto j : B[i]) {
        if (j.first + B[minid].front().first <= Y) {
          e[j.second].push_back(B[minid][0].second);
          e[B[minid][0].second].push_back(j.second);
        }
      }
    }
  }
  ll _ = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      ll res = 0, tmp = 1;
      for (auto j : clr) {
        res += cnt[j];
        tmp = tmp * _inv[cnt[j]] % ljc;
        cnt[j] = 0;
      }
      _ = _ * tmp % ljc * fac[res] % ljc;
      clr.clear();
    }
  }
  cout << _;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define ljc 1000000007
using namespace std;
#define gc getchar
inline ll read() {
  register ll x = 0, f = 1;
  char ch = gc();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = gc();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = gc();
  }
  return (f == 1) ? x : -x;
}
vector<pair<int, int>> B[201010];
vector<int> clr, e[201010];
int n, col[201010], w[201010], X, Y, cnt[201010], fac[201010], inv[201010],
    _inv[201010];
bool vis[201010];
void dfs(int u) {
  vis[u] = 1;
  cnt[col[u]]++;
  clr.push_back(col[u]);
  for (auto v : e[u]) {
    if (!vis[v])
      dfs(v);
  }
}
signed main() {
  n = read(), X = read(), Y = read();
  fac[0] = inv[0] = inv[1] = _inv[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % ljc;
  for (int i = 2; i <= n; i++)
    inv[i] = (ljc - (ljc / i) * inv[ljc % i] % ljc) % ljc;
  for (int i = 1; i <= n; i++)
    _inv[i] = _inv[i - 1] * inv[i] % ljc;
  for (int i = 1; i <= n; i++) {
    col[i] = read(), w[i] = read();
    B[col[i]].push_back(make_pair(w[i], i));
  }
  for (int i = 1; i <= n; i++) {
    if (B[i].size())
      sort(B[i].begin(), B[i].end());
    for (int j = 1; j < (int)B[i].size(); j++) {
      if (B[i][j].first + B[i][0].first <= X) {
        e[B[i][j].second].push_back(B[i][0].second);
        e[B[i][0].second].push_back(B[i][j].second);
      }
    }
  }
  int minid = 0, min2id = 0;
  B[0].push_back(make_pair(1000000001, 0));
  for (int i = 1; i <= n; i++) {
    if (B[i].size()) {
      if (B[i].front().first < B[minid].front().first) {
        min2id = minid, minid = i;
      } else if (B[i].front().first < B[min2id].front().first) {
        min2id = i;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == minid) {
      for (auto j : B[i]) {
        if (j.first + B[min2id].front().first <= Y) {
          e[j.second].push_back(B[min2id][0].second);
          e[B[min2id][0].second].push_back(j.second);
        }
      }
    } else {
      for (auto j : B[i]) {
        if (j.first + B[minid].front().first <= Y) {
          e[j.second].push_back(B[minid][0].second);
          e[B[minid][0].second].push_back(j.second);
        }
      }
    }
  }
  ll _ = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      ll res = 0, tmp = 1;
      for (auto j : clr) {
        res += cnt[j];
        tmp = tmp * _inv[cnt[j]] % ljc;
        cnt[j] = 0;
      }
      _ = _ * tmp % ljc * fac[res] % ljc;
      clr.clear();
    }
  }
  cout << _;
  return 0;
}