Problem p03877 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define P 1000000007
int N, A, B, C;
int co[5010][5010];
int dp[2501][5010][2];
int ans;

void up(int &x, int y) { x = (x + y) % P; }

int main() {
  cin >> N >> A >> B >> C;
  co[0][0] = 1;
  for (int i = 1; i <= 5000; i++) {
    co[i][0] = 1;
    for (int j = 1; j <= i; j++)
      co[i][j] = (co[i - 1][j] + co[i - 1][j - 1]) % P;
  }
  if (B % 2 != 0) {
    printf("0\n");
    return 0;
  }
  dp[0][0][0] = 1;
  for (int i = 0; i <= N / 2; i++)
    for (int j = 0; j <= N; j++) {
      if (dp[i][j][1]) {
        up(dp[i][j][0], dp[i][j][1]);
        up(dp[i][j + 1][1], dp[i][j][1]);
      }
      if (dp[i][j][0]) {
        up(dp[i + 1][j][1], dp[i][j][0]);
        // up(dp[i + 1][j][0], dp[i][j][0]);
      }
    }
  // for (int i = 0; i <= N / 3; i++)
  for (int j = 0; j <= C; j++)
    if (dp[B / 2][j][0])
      for (int q = 0; j + 3 * q <= C; q++) {
        // if ((B - 2 * i) % 2 != 0)
        // 	continue;
        int aa = C - j - 3 * q, bb = q, cc = A - aa;
        // printf("?? %d %d %d %d %d\n", i, j, aa, bb, cc);
        if (aa < 0 || cc < 0)
          continue;
        // printf("?? %d %d %d %d %d %d\n", i, j, aa, bb, cc, dp[i][j][0]);

        int tmp = dp[B / 2][j][0];
        tmp = 1LL * tmp * co[B / 2 + aa][aa] % P;
        tmp = 1LL * tmp * co[B / 2 + aa + bb][bb] % P;
        tmp = 1LL * tmp * co[B / 2 + aa + bb + cc][cc] % P;
        // tmp = 1LL * tmp * co[i + aa + bb + cc][cc] % P;
        // tmp = 1LL * tmp * co[i + aa + bb + cc + q][q] % P;

        up(ans, tmp);
      }
  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define P 1000000007
int N, A, B, C;
int co[5010][5010];
int dp[2511][5010][2];
int ans;

void up(int &x, int y) { x = (x + y) % P; }

int main() {
  cin >> N >> A >> B >> C;
  co[0][0] = 1;
  for (int i = 1; i <= 5000; i++) {
    co[i][0] = 1;
    for (int j = 1; j <= i; j++)
      co[i][j] = (co[i - 1][j] + co[i - 1][j - 1]) % P;
  }
  if (B % 2 != 0) {
    printf("0\n");
    return 0;
  }
  dp[0][0][0] = 1;
  for (int i = 0; i <= N / 2; i++)
    for (int j = 0; j <= N; j++) {
      if (dp[i][j][1]) {
        up(dp[i][j][0], dp[i][j][1]);
        up(dp[i][j + 1][1], dp[i][j][1]);
      }
      if (dp[i][j][0]) {
        up(dp[i + 1][j][1], dp[i][j][0]);
        // up(dp[i + 1][j][0], dp[i][j][0]);
      }
    }
  // for (int i = 0; i <= N / 3; i++)
  for (int j = 0; j <= C; j++)
    if (dp[B / 2][j][0])
      for (int q = 0; j + 3 * q <= C; q++) {
        // if ((B - 2 * i) % 2 != 0)
        // 	continue;
        int aa = C - j - 3 * q, bb = q, cc = A - aa;
        // printf("?? %d %d %d %d %d\n", i, j, aa, bb, cc);
        if (aa < 0 || cc < 0)
          continue;
        // printf("?? %d %d %d %d %d %d\n", i, j, aa, bb, cc, dp[i][j][0]);

        int tmp = dp[B / 2][j][0];
        tmp = 1LL * tmp * co[B / 2 + aa][aa] % P;
        tmp = 1LL * tmp * co[B / 2 + aa + bb][bb] % P;
        tmp = 1LL * tmp * co[B / 2 + aa + bb + cc][cc] % P;
        // tmp = 1LL * tmp * co[i + aa + bb + cc][cc] % P;
        // tmp = 1LL * tmp * co[i + aa + bb + cc + q][q] % P;

        up(ans, tmp);
      }
  printf("%d\n", ans);
}