Problem p03770 (C++)

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

using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, v) for (auto &a : v)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;

ll big = 1000000007ll;
ll big2 = 1000000009ll;
ll n, m, q, T, k;
ll x, y;

vector<ll> C;
vector<ll> W;
vector<ll> CW;

bool ACC[200002] = {0};

ll minw = big * big;
ll i = 0;

ll FAC[200002] = {0};
ll INV[200002] = {0};

ll F[200002] = {0};

ll upp(ll a, ll i) {
  if (i == 0)
    return 1;
  if (i % 2 == 0) {
    ll h = upp(a, i / 2);
    return (h * h) % big;
  }
  return (upp(a, i - 1) * a) % big;
}

void setup() {

  ll t = 1;

  for (ll c1 = 0; c1 < 200002; c1++) {
    FAC[c1] = t;
    INV[c1] = upp(t, big - 2);
    t *= c1 + 1;
    t %= big;
  }
}

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("autput.txt","w",stdout);

  ll a, b, c;
  setup();
  cin >> n >> x >> y;

  for (ll c1 = 0; c1 < 200002; c1++) {
    CW.push_back(big * big);
  }

  for (ll c1 = 0; c1 < n; c1++) {
    cin >> a >> b;
    C.push_back(a);
    W.push_back(b);
    CW[a] = min(b, CW[a]);
    if (b < minw) {
      minw = b;
      i = c1;
    }
  }
  ll minw2 = big * big;
  ll j = 0;

  for (ll c1 = 0; c1 < n; c1++) {
    if (C[c1] != C[i] && W[c1] < minw2) {
      minw2 = W[b];
      j = c1;
    }
  }

  for (ll c1 = 0; c1 < 200002; c1++) {
    if (c1 != C[i]) {
      if (CW[c1] + W[i] <= y)
        ACC[c1] = 1;
    }
  }

  ll p = 0;

  for (ll c1 = 0; c1 < n; c1++) {

    if (C[c1] == C[i]) {
      if (W[c1] + W[i] <= x || W[c1] + W[j] <= y) {
        F[C[c1]]++;
        p++;
      }
    } else {
      if ((W[c1] + CW[C[c1]] <= x || W[c1] + W[i] <= y) && ACC[C[c1]]) {
        F[C[c1]]++;
        p++;
      }
    }
  }

  ll ans = FAC[p];

  for (ll c1 = 0; c1 < 200002; c1++) {
    ans *= INV[F[c1]];
    ans %= big;
  }

  cout << ans << "\n";

  return 0;
}


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

using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, v) for (auto &a : v)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;

ll big = 1000000007ll;
ll big2 = 1000000009ll;
ll n, m, q, T, k;
ll x, y;

vector<ll> C;
vector<ll> W;
vector<ll> CW;

bool ACC[200002] = {0};

ll minw = big * big;
ll i = 0;

ll FAC[200002] = {0};
ll INV[200002] = {0};

ll F[200002] = {0};

ll upp(ll a, ll i) {
  if (i == 0)
    return 1;
  if (i % 2 == 0) {
    ll h = upp(a, i / 2);
    return (h * h) % big;
  }
  return (upp(a, i - 1) * a) % big;
}

void setup() {

  ll t = 1;

  for (ll c1 = 0; c1 < 200002; c1++) {
    FAC[c1] = t;
    INV[c1] = upp(t, big - 2);
    t *= c1 + 1;
    t %= big;
  }
}

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("autput.txt","w",stdout);

  ll a, b, c;
  setup();
  cin >> n >> x >> y;

  for (ll c1 = 0; c1 < 200002; c1++) {
    CW.push_back(big * big);
  }

  for (ll c1 = 0; c1 < n; c1++) {
    cin >> a >> b;
    C.push_back(a);
    W.push_back(b);
    CW[a] = min(b, CW[a]);
    if (b < minw) {
      minw = b;
      i = c1;
    }
  }
  ll minw2 = big * big;
  ll j = 0;

  for (ll c1 = 0; c1 < n; c1++) {
    if (C[c1] != C[i] && W[c1] < minw2) {
      minw2 = W[c1];
      j = c1;
    }
  }

  for (ll c1 = 0; c1 < 200002; c1++) {
    if (c1 != C[i]) {
      if (CW[c1] + W[i] <= y)
        ACC[c1] = 1;
    }
  }

  ll p = 0;

  for (ll c1 = 0; c1 < n; c1++) {

    if (C[c1] == C[i]) {
      if (W[c1] + W[i] <= x || W[c1] + W[j] <= y) {
        F[C[c1]]++;
        p++;
      }
    } else {
      if ((W[c1] + CW[C[c1]] <= x || W[c1] + W[i] <= y) && ACC[C[c1]]) {
        F[C[c1]]++;
        p++;
      }
    }
  }

  ll ans = FAC[p];

  for (ll c1 = 0; c1 < 200002; c1++) {
    ans *= INV[F[c1]];
    ans %= big;
  }

  cout << ans << "\n";

  return 0;
}