Problem p03770 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long read() {
  char ch = getchar();
  long long x = 0, ff = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * ff;
}
void write(long long aa) {
  if (aa < 0)
    putchar('-'), aa = -aa;
  if (aa > 9)
    write(aa / 10);
  putchar(aa % 10 + '0');
  return;
}
const long long inf = 1000000005;
const long long mod = 1e9 + 7;
long long n, X, Y, sum, ans;
long long jl[200005], mn[200005], val[200005];
long long fac[200005], ny[200005], col[200005];
long long ksm(long long aa, long long bb) {
  long long ans = 1;
  while (bb) {
    if (bb & 1)
      ans = (ans * aa) % mod;
    bb >>= 1;
    aa = (aa * aa) % mod;
  }
  return ans;
}
long long C(long long aa, long long bb) {
  return fac[bb] * ny[aa] % mod * ny[bb - aa] % mod;
}
int main() {
  freopen("keep.in", "r", stdin);
  freopen("keep.out", "w", stdout);
  n = read(), X = read(), Y = read();
  fac[0] = 1;
  for (long long i = 1; i <= n; ++i) {
    col[i] = read(), val[i] = read();
    jl[col[i]]++;
    mn[i] = inf;
    fac[i] = (fac[i - 1] * i) % mod;
  }
  ny[n] = ksm(fac[n], mod - 2);
  for (long long i = n; i >= 1; --i)
    ny[i - 1] = (ny[i] * i) % mod;
  for (long long i = 1; i <= n; ++i)
    mn[col[i]] = min(mn[col[i]], val[i]);
  long long mx = 0, mxx = 0;
  for (long long i = 1; i <= n; ++i)
    if (mn[i] != inf) {
      if (!mx || mn[i] < mn[mx])
        mxx = mx, mx = i;
      else if (!mxx || mn[i] < mn[mxx])
        mxx = i;
    }
  for (long long i = 1; i <= n; ++i) {
    if (mn[col[i]] != val[i] && mn[col[i]] + val[i] <= X)
      continue;
    if (col[i] == mx && val[i] + mn[mxx] <= Y)
      continue;
    if (col[i] != mx && val[i] + mn[mx] <= Y)
      continue;
    jl[col[i]]--;
  }
  ans = 1;
  sum = jl[mx];
  for (long long i = 1; i <= n; ++i)
    if (i != mx && mn[i] + mn[mx] <= Y) {
      sum += jl[i];
      ans = (ans * C(jl[i], sum)) % mod;
    }
  write(ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long read() {
  char ch = getchar();
  long long x = 0, ff = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * ff;
}
void write(long long aa) {
  if (aa < 0)
    putchar('-'), aa = -aa;
  if (aa > 9)
    write(aa / 10);
  putchar(aa % 10 + '0');
  return;
}
const long long inf = 1000000005;
const long long mod = 1e9 + 7;
long long n, X, Y, sum, ans;
long long jl[200005], mn[200005], val[200005];
long long fac[200005], ny[200005], col[200005];
long long ksm(long long aa, long long bb) {
  long long ans = 1;
  while (bb) {
    if (bb & 1)
      ans = (ans * aa) % mod;
    bb >>= 1;
    aa = (aa * aa) % mod;
  }
  return ans;
}
long long C(long long aa, long long bb) {
  return fac[bb] * ny[aa] % mod * ny[bb - aa] % mod;
}
int main() {
  n = read(), X = read(), Y = read();
  fac[0] = 1;
  for (long long i = 1; i <= n; ++i) {
    col[i] = read(), val[i] = read();
    jl[col[i]]++;
    mn[i] = inf;
    fac[i] = (fac[i - 1] * i) % mod;
  }
  ny[n] = ksm(fac[n], mod - 2);
  for (long long i = n; i >= 1; --i)
    ny[i - 1] = (ny[i] * i) % mod;
  for (long long i = 1; i <= n; ++i)
    mn[col[i]] = min(mn[col[i]], val[i]);
  long long mx = 0, mxx = 0;
  for (long long i = 1; i <= n; ++i)
    if (mn[i] != inf) {
      if (!mx || mn[i] < mn[mx])
        mxx = mx, mx = i;
      else if (!mxx || mn[i] < mn[mxx])
        mxx = i;
    }
  for (long long i = 1; i <= n; ++i) {
    if (mn[col[i]] != val[i] && mn[col[i]] + val[i] <= X)
      continue;
    if (col[i] == mx && val[i] + mn[mxx] <= Y)
      continue;
    if (col[i] != mx && val[i] + mn[mx] <= Y)
      continue;
    jl[col[i]]--;
  }
  ans = 1;
  sum = jl[mx];
  for (long long i = 1; i <= n; ++i)
    if (i != mx && mn[i] + mn[mx] <= Y) {
      sum += jl[i];
      ans = (ans * C(jl[i], sum)) % mod;
    }
  write(ans);
  return 0;
}