Problem p04051 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 10000000

// aのb乗
int beki(int a, int b, int M = modulo) {
  int x = 1;
  while (b != 0) {
    if (b & 1) {
      x = ((long long)x * a) % M;
    }
    a = ((long long)a * a) % M;
    b >>= 1;
  }
  return x;
}

// aの逆元
int gyakugen(int a) { return beki(a, modulo - 2); }

struct combi {
  deque<int> kaijou;
  deque<int> kaijou_;

  combi(int n) {
    kaijou.push_back(1);
    for (int i = 1; i <= n; i++) {
      kaijou.push_back(mod(kaijou[i - 1] * i));
    }

    int b = gyakugen(kaijou[n]);

    kaijou_.push_front(b);
    for (int i = 1; i <= n; i++) {
      int k = n + 1 - i;
      kaijou_.push_front(mod(kaijou_[0] * k));
    }
  }

  int combination(int n, int r) {
    if (r > n)
      return 0;
    int a = mod(kaijou[n] * kaijou_[r]);
    a = mod(a * kaijou_[n - r]);
    return a;
  }

  int junretsu(int a, int b) {
    int x = mod(kaijou_[a] * kaijou_[b]);
    x = mod(x * kaijou[a + b]);
    return x;
  }
};

int main() {

  int N;
  cin >> N;

  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &A[i], &B[i]);
  }

  vector<vector<int>> dp(4001, vector<int>(4001, 0));

  for (int i = 0; i < N; i++) {
    dp[2000 - A[i]][2000 - B[i]]++;
  }

  for (int i = 0; i <= 4000; i++) {
    for (int j = 0; j <= 4000; j++) {
      if (i != 4000) {
        dp[i + 1][j] = mod(dp[i][j] + dp[i + 1][j]);
      }
      if (j != 4000) {
        dp[i][j + 1] = mod(dp[i][j] + dp[i][j + 1]);
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < N; i++) {
    ans = mod(ans + dp[2000 + A[i]][2000 + B[i]]);
  }

  combi C(5000);

  for (int i = 0; i < N; i++) {
    ans = mod(ans - C.junretsu(A[i] * 2, B[i] * 2));
  }

  ans = mod(ans * gyakugen(2));

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 10000000

// aのb乗
int beki(int a, int b, int M = modulo) {
  int x = 1;
  while (b != 0) {
    if (b & 1) {
      x = ((long long)x * a) % M;
    }
    a = ((long long)a * a) % M;
    b >>= 1;
  }
  return x;
}

// aの逆元
int gyakugen(int a) { return beki(a, modulo - 2); }

struct combi {
  deque<int> kaijou;
  deque<int> kaijou_;

  combi(int n) {
    kaijou.push_back(1);
    for (int i = 1; i <= n; i++) {
      kaijou.push_back(mod(kaijou[i - 1] * i));
    }

    int b = gyakugen(kaijou[n]);

    kaijou_.push_front(b);
    for (int i = 1; i <= n; i++) {
      int k = n + 1 - i;
      kaijou_.push_front(mod(kaijou_[0] * k));
    }
  }

  int combination(int n, int r) {
    if (r > n)
      return 0;
    int a = mod(kaijou[n] * kaijou_[r]);
    a = mod(a * kaijou_[n - r]);
    return a;
  }

  int junretsu(int a, int b) {
    int x = mod(kaijou_[a] * kaijou_[b]);
    x = mod(x * kaijou[a + b]);
    return x;
  }
};

int main() {

  int N;
  cin >> N;

  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &A[i], &B[i]);
  }

  vector<vector<int>> dp(4001, vector<int>(4001, 0));

  for (int i = 0; i < N; i++) {
    dp[2000 - A[i]][2000 - B[i]]++;
  }

  for (int i = 0; i <= 4000; i++) {
    for (int j = 0; j <= 4000; j++) {
      if (i != 4000) {
        dp[i + 1][j] = mod(dp[i][j] + dp[i + 1][j]);
      }
      if (j != 4000) {
        dp[i][j + 1] = mod(dp[i][j] + dp[i][j + 1]);
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < N; i++) {
    ans = mod(ans + dp[2000 + A[i]][2000 + B[i]]);
  }

  combi C(10000);

  for (int i = 0; i < N; i++) {
    ans = mod(ans - C.junretsu(A[i] * 2, B[i] * 2));
  }

  ans = mod(ans * gyakugen(2));

  cout << ans << endl;

  return 0;
}