Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;
const int N = 1e5 + 10;

int n, x, y;
int c[N], w[N], colormin[N], smallest, secondsmallest;

int par[N], sz[N], inv[N];
map<int, int> exi[N];
int find(int x) {
  if (par[x] != x) {
    return par[x] = find(par[x]);
  }
  return par[x];
}

void merge(int x, int y) {
  //	printf("[%d %d]\n",x,y);
  x = find(x), y = find(y);
  if (x ^ y) {
    par[x] = y;
  }
}
int main() {

  scanf("%d%d%d", &n, &x, &y);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", c + i, w + i);
    par[i] = i;

    if (!colormin[c[i]] || w[colormin[c[i]]] > w[i])
      colormin[c[i]] = i;
    if (!smallest || w[smallest] > w[i]) {
      smallest = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (c[i] == c[smallest])
      continue;
    if (!secondsmallest || w[secondsmallest] > w[i]) {
      secondsmallest = i;
    }
  }

  //	printf("[%d %d] (%d
  //%d)\n",smallest,secondsmallest,colormin[c[smallest]],colormin[c[secondsmallest]]);

  inv[1] = 1;
  for (int i = 2; i <= n; i++)
    inv[i] = 1ll * (mod - (mod / i)) * inv[mod % i] % mod;

  for (int i = 1; i <= n; i++) {
    if (w[i] + w[colormin[c[i]]] <= x)
      merge(i, colormin[c[i]]);

    if (smallest)
      if (c[i] != c[smallest] && w[i] + w[smallest] <= y) {
        merge(i, smallest);
      }

    if (secondsmallest)
      if (c[i] != c[secondsmallest] && w[i] + w[secondsmallest] <= y) {
        merge(i, secondsmallest);
      }
  }
  int res = 1;
  for (int i = 1; i <= n; i++) {
    res = 1ll * res * (++sz[find(i)]) % mod;
    res = 1ll * res * inv[++exi[find(i)][c[i]]] % mod;

    //		printf("[%d %d]\n",sz[find(i)] , exi[find(i)][c[i]]);
  }
  cout << res;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;
const int N = 2e5 + 10;

int n, x, y;
int c[N], w[N], colormin[N], smallest, secondsmallest;

int par[N], sz[N], inv[N];
map<int, int> exi[N];
int find(int x) {
  if (par[x] != x) {
    return par[x] = find(par[x]);
  }
  return par[x];
}

void merge(int x, int y) {
  //	printf("[%d %d]\n",x,y);
  x = find(x), y = find(y);
  if (x ^ y) {
    par[x] = y;
  }
}
int main() {

  scanf("%d%d%d", &n, &x, &y);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", c + i, w + i);
    par[i] = i;

    if (!colormin[c[i]] || w[colormin[c[i]]] > w[i])
      colormin[c[i]] = i;
    if (!smallest || w[smallest] > w[i]) {
      smallest = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (c[i] == c[smallest])
      continue;
    if (!secondsmallest || w[secondsmallest] > w[i]) {
      secondsmallest = i;
    }
  }

  //	printf("[%d %d] (%d
  //%d)\n",smallest,secondsmallest,colormin[c[smallest]],colormin[c[secondsmallest]]);

  inv[1] = 1;
  for (int i = 2; i <= n; i++)
    inv[i] = 1ll * (mod - (mod / i)) * inv[mod % i] % mod;

  for (int i = 1; i <= n; i++) {
    if (w[i] + w[colormin[c[i]]] <= x)
      merge(i, colormin[c[i]]);

    if (smallest)
      if (c[i] != c[smallest] && w[i] + w[smallest] <= y) {
        merge(i, smallest);
      }

    if (secondsmallest)
      if (c[i] != c[secondsmallest] && w[i] + w[secondsmallest] <= y) {
        merge(i, secondsmallest);
      }
  }
  int res = 1;
  for (int i = 1; i <= n; i++) {
    res = 1ll * res * (++sz[find(i)]) % mod;
    res = 1ll * res * inv[++exi[find(i)][c[i]]] % mod;

    //		printf("[%d %d]\n",sz[find(i)] , exi[find(i)][c[i]]);
  }
  cout << res;
  return 0;
}