Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int MOD = 1e9 + 7;

int par[N], sz[N];
int val[N];
int fact[N];
vector<int> all[N];

int root(int v) { return par[v] == -1 ? v : par[v] = root(par[v]); }
void merge(int v, int u) {
  v = root(v);
  u = root(u);
  if (v == u)
    return;
  par[v] = u;
  sz[u] += sz[v];
}

bool cmp(int a, int b) { return val[a] < val[b]; }

int bpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = 1LL * res * a % MOD;
    a = 1LL * a * a % MOD;
    b >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);

  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = 1LL * i * fact[i - 1] % MOD;
  for (int i = 0; i < N; i++)
    sz[i] = 1, par[i] = -1;

  int n, A, B;
  cin >> n >> A >> B;
  for (int i = 0; i < n; i++) {
    int c;
    cin >> c >> val[i];
    all[--c].push_back(i);
  }
  for (int i = 0; i < N; i++)
    sort(all[i].begin(), all[i].end(), cmp);
  int mn = -1, mn2 = -1;
  for (int i = 0; i < N; i++)
    if (!all[i].empty()) {
      if (mn == -1 || val[all[mn][0]] > val[all[i][0]])
        mn2 = mn, mn = i;
      else if (mn2 == -1 || val[all[mn2][0]] > val[all[i][0]])
        mn2 = i;
    }
  for (int i = 0; i < N; i++) {
    for (auto x : all[i]) {
      if (val[all[i][0]] + val[x] <= A)
        merge(all[i][0], x);
      if (mn != -1 && mn != i) {
        if (val[all[mn][0]] + val[x] <= B)
          merge(all[mn][0], x);
      } else if (mn2 != -1 && mn2 != i) {
        if (val[all[mn2][0]] + val[x] <= B)
          merge(all[mn2][0], x);
      }
    }
  }
  int res = 1;
  for (int i = 0; i < n; i++)
    if (par[i] == -1)
      res = 1LL * res * fact[sz[i]] % MOD;
  for (int i = 0; i < N; i++) {
    map<int, int> mp;
    for (auto x : all[i])
      mp[root(x)]++;
    for (auto &x : mp)
      res = 1LL * res * bpow(fact[x.second], MOD - 2) % MOD;
  }
  cout << res << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
const int MOD = 1e9 + 7;

int par[N], sz[N];
int val[N];
int fact[N];
vector<int> all[N];

int root(int v) { return par[v] == -1 ? v : par[v] = root(par[v]); }
void merge(int v, int u) {
  v = root(v);
  u = root(u);
  if (v == u)
    return;
  par[v] = u;
  sz[u] += sz[v];
}

bool cmp(int a, int b) { return val[a] < val[b]; }

int bpow(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = 1LL * res * a % MOD;
    a = 1LL * a * a % MOD;
    b >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);

  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = 1LL * i * fact[i - 1] % MOD;
  for (int i = 0; i < N; i++)
    sz[i] = 1, par[i] = -1;

  int n, A, B;
  cin >> n >> A >> B;
  for (int i = 0; i < n; i++) {
    int c;
    cin >> c >> val[i];
    all[--c].push_back(i);
  }
  for (int i = 0; i < N; i++)
    sort(all[i].begin(), all[i].end(), cmp);
  int mn = -1, mn2 = -1;
  for (int i = 0; i < N; i++)
    if (!all[i].empty()) {
      if (mn == -1 || val[all[mn][0]] > val[all[i][0]])
        mn2 = mn, mn = i;
      else if (mn2 == -1 || val[all[mn2][0]] > val[all[i][0]])
        mn2 = i;
    }
  for (int i = 0; i < N; i++) {
    for (auto x : all[i]) {
      if (val[all[i][0]] + val[x] <= A)
        merge(all[i][0], x);
      if (mn != -1 && mn != i) {
        if (val[all[mn][0]] + val[x] <= B)
          merge(all[mn][0], x);
      } else if (mn2 != -1 && mn2 != i) {
        if (val[all[mn2][0]] + val[x] <= B)
          merge(all[mn2][0], x);
      }
    }
  }
  int res = 1;
  for (int i = 0; i < n; i++)
    if (par[i] == -1)
      res = 1LL * res * fact[sz[i]] % MOD;
  for (int i = 0; i < N; i++) {
    map<int, int> mp;
    for (auto x : all[i])
      mp[root(x)]++;
    for (auto &x : mp)
      res = 1LL * res * bpow(fact[x.second], MOD - 2) % MOD;
  }
  cout << res << "\n";
  return 0;
}