Problem p04051 (C++)

Buggy submission (status: Runtime Error):
//                             In The Name Of Allah
//                             	Mohammad Hosseini
#include <bits/stdc++.h>
#define ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
#define int long long
#define ld long double
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 1e4 + 100, OO = 724249387, T = 4500 + 100, M = 1e9 + 7, P = 6151,
          SQ = 280, lg = 30;
typedef pair<int, int> pii;
int fac[N], inv[N], dp[T][T], is[T][T], it[T][T];

int pw(int x, int y) {
  if (y == 0)
    return 1;
  int cnt = pw(x, y / 2);
  cnt = (cnt * cnt) % M;
  cnt = (cnt * (y % 2 == 1 ? x : 1)) % M;
  return cnt;
}

void prepro() {
  fac[0] = 1;
  for (int i = 1; i <= N - lg; i++)
    fac[i] = (fac[i - 1] * i) % M;
  inv[N - lg] = pw(fac[N - lg], M - 2);
  for (int i = N - lg - 1; i > -1; i--)
    inv[i] = (inv[i + 1] * (i + 1)) % M;
}

int c(int x, int y) { return ((fac[x] * inv[y] % M) * inv[x - y]) % M; }

int32_t main() {
  use_fast;
  prepro();
  int n, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    cnt -= c(2 * (x + y), 2 * x);
    cnt = (cnt % M + M) % M;
    is[-x + 2500][-y + 2500]++;
    it[x + 2500][y + 2500]++;
  }
  for (int i = 1; i <= 5000; i++) {
    for (int j = 1; j <= 5000; j++) {
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] + is[i][j]) % M;
      cnt = (cnt + dp[i][j] * it[i][j]) % M;
    }
  }
  cnt = (cnt * pw(2, M - 2)) % M;
  cout << cnt << endl;
  return 0;
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- uploaded by ubuntu 20.04
5- ...
*/


Fixed submission:
//                             In The Name Of Allah
//                             	Mohammad Hosseini
#include <bits/stdc++.h>
#define ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
#define int long long
#define ld long double
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 1e4 + 100, OO = 724249387, T = 4500 + 100, M = 1e9 + 7, P = 6151,
          SQ = 280, lg = 30;
typedef pair<int, int> pii;
int fac[N], inv[N], dp[T][T], is[T][T], it[T][T];

int pw(int x, int y) {
  if (y == 0)
    return 1;
  int cnt = pw(x, y / 2);
  cnt = (cnt * cnt) % M;
  cnt = (cnt * (y % 2 == 1 ? x : 1)) % M;
  return cnt;
}

void prepro() {
  fac[0] = 1;
  for (int i = 1; i <= N - lg; i++)
    fac[i] = (fac[i - 1] * i) % M;
  inv[N - lg] = pw(fac[N - lg], M - 2);
  for (int i = N - lg - 1; i > -1; i--)
    inv[i] = (inv[i + 1] * (i + 1)) % M;
}

int c(int x, int y) { return ((fac[x] * inv[y] % M) * inv[x - y]) % M; }

int32_t main() {
  use_fast;
  prepro();
  int n, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    cnt -= c(2 * (x + y), 2 * x);
    cnt = (cnt % M + M) % M;
    is[-x + 2500][-y + 2500]++;
    it[x + 2500][y + 2500]++;
  }
  for (int i = 1; i <= 4550; i++) {
    for (int j = 1; j <= 4550; j++) {
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] + is[i][j]) % M;
      cnt = (cnt + dp[i][j] * it[i][j]) % M;
    }
  }
  cnt = (cnt * pw(2, M - 2)) % M;
  cout << cnt << endl;
  return 0;
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- uploaded by ubuntu 20.04
5- ...
*/