Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y)                                                           \
  for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y)                                                          \
  for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(x...)                                                              \
  do {                                                                         \
    cout << "\033[32;1m" << #x << " -> ";                                      \
    err(x);                                                                    \
  } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <template <typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
  for (auto v : a)
    cout << v << ' ';
  err(x...);
}
template <typename T, typename... A> void err(T a, A... x) {
  cout << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif
// -----------------------------------------------------------------------------

typedef pair<int, int> P;
const int N = 1E5 + 100, MOD = 1E9 + 7;
vector<P> a[N];
int c[N], w[N], n, x, y;
map<int, int> b[N];

int fa[N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy)
    fa[fx] = fy;
}

void connect(int color) {
  int wt = y - a[color].front().first;
  int id = a[color].front().second;
  FOR(i, 1, n + 1) {
    if (c[i] != color && w[i] <= wt)
      merge(i, id);
  }
}

void connect_self(int color) {
  if (a[color].empty())
    return;
  P p = a[color][0];
  FOR(i, 1, a[color].size()) {
    const P &q = a[color][i];
    if (p.first + q.first <= x)
      merge(p.second, q.second);
  }
}

LL bin(LL x, LL n, LL MOD) {
  LL ret = MOD != 1;
  for (x %= MOD; n; n >>= 1, x = x * x % MOD)
    if (n & 1)
      ret = ret * x % MOD;
  return ret;
}

LL invf[N], fac[N];
void fac_inv_init(LL n, LL p) {
  fac[0] = 1;
  FOR(i, 1, n)
  fac[i] = i * fac[i - 1] % p;
  invf[n - 1] = bin(fac[n - 1], p - 2, p);
  FORD(i, n - 2, -1)
  invf[i] = invf[i + 1] * (i + 1) % p;
}

int main() {
  fac_inv_init(N, MOD);
  cin >> n >> x >> y;
  FOR(i, 1, n + 1) {
    cin >> c[i] >> w[i];
    a[c[i]].emplace_back(w[i], i);
  }
  FOR(i, 1, n + 1) { sort(a[i].begin(), a[i].end()); }
  int min1 = -1, min2 = -1;
  FOR(i, 1, n + 1) {
    if (!a[i].empty() &&
        (min1 == -1 || a[i].front().first < a[min1].front().first))
      min1 = i;
  }
  FOR(i, 1, n + 1) {
    if (i == min1)
      continue;
    if (!a[i].empty() &&
        (min2 == -1 || a[i].front().first < a[min2].front().first))
      min2 = i;
  }
  dbg(min1, min2);

  iota(fa, fa + N, 0);
  if (min1 != -1)
    connect(min1);
  if (min2 != -1)
    connect(min2);
  FOR(i, 1, n + 1)
  connect_self(i);

  FOR(i, 1, n + 1)
  b[find(i)][c[i]]++;

  LL ans = 1;
  FOR(i, 1, n + 1) {
    if (!b[i].empty()) {
      LL s = 0;
      for (auto &p : b[i]) {
        ans = ans * invf[p.second] % MOD;
        (s += p.second) %= MOD;
      }
      ans = ans * fac[s] % MOD;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y)                                                           \
  for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y)                                                          \
  for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(x...)                                                              \
  do {                                                                         \
    cout << "\033[32;1m" << #x << " -> ";                                      \
    err(x);                                                                    \
  } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <template <typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
  for (auto v : a)
    cout << v << ' ';
  err(x...);
}
template <typename T, typename... A> void err(T a, A... x) {
  cout << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif
// -----------------------------------------------------------------------------

typedef pair<int, int> P;
const int N = 2E5 + 100, MOD = 1E9 + 7;
vector<P> a[N];
int c[N], w[N], n, x, y;
map<int, int> b[N];

int fa[N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy)
    fa[fx] = fy;
}

void connect(int color) {
  int wt = y - a[color].front().first;
  int id = a[color].front().second;
  FOR(i, 1, n + 1) {
    if (c[i] != color && w[i] <= wt)
      merge(i, id);
  }
}

void connect_self(int color) {
  if (a[color].empty())
    return;
  P p = a[color][0];
  FOR(i, 1, a[color].size()) {
    const P &q = a[color][i];
    if (p.first + q.first <= x)
      merge(p.second, q.second);
  }
}

LL bin(LL x, LL n, LL MOD) {
  LL ret = MOD != 1;
  for (x %= MOD; n; n >>= 1, x = x * x % MOD)
    if (n & 1)
      ret = ret * x % MOD;
  return ret;
}

LL invf[N], fac[N];
void fac_inv_init(LL n, LL p) {
  fac[0] = 1;
  FOR(i, 1, n)
  fac[i] = i * fac[i - 1] % p;
  invf[n - 1] = bin(fac[n - 1], p - 2, p);
  FORD(i, n - 2, -1)
  invf[i] = invf[i + 1] * (i + 1) % p;
}

int main() {
  fac_inv_init(N, MOD);
  cin >> n >> x >> y;
  FOR(i, 1, n + 1) {
    cin >> c[i] >> w[i];
    a[c[i]].emplace_back(w[i], i);
  }
  FOR(i, 1, n + 1) { sort(a[i].begin(), a[i].end()); }
  int min1 = -1, min2 = -1;
  FOR(i, 1, n + 1) {
    if (!a[i].empty() &&
        (min1 == -1 || a[i].front().first < a[min1].front().first))
      min1 = i;
  }
  FOR(i, 1, n + 1) {
    if (i == min1)
      continue;
    if (!a[i].empty() &&
        (min2 == -1 || a[i].front().first < a[min2].front().first))
      min2 = i;
  }
  dbg(min1, min2);

  iota(fa, fa + N, 0);
  if (min1 != -1)
    connect(min1);
  if (min2 != -1)
    connect(min2);
  FOR(i, 1, n + 1)
  connect_self(i);

  FOR(i, 1, n + 1)
  b[find(i)][c[i]]++;

  LL ans = 1;
  FOR(i, 1, n + 1) {
    if (!b[i].empty()) {
      LL s = 0;
      for (auto &p : b[i]) {
        ans = ans * invf[p.second] % MOD;
        (s += p.second) %= MOD;
      }
      ans = ans * fac[s] % MOD;
    }
  }
  cout << ans << endl;
}