Problem p03581 (C++)

Buggy submission (status: Runtime Error):
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long long llong;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
        ll ans = 1; while (b) {
                while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
                ans = (ans * a) % MOD, --b;
        } return ans;
}
*/
const int MX = 2010;
const ll MOD = 1e9 + 7;

int a, b;
int cnk[MX][MX];
int dp1[MX][MX];
int cc[MX][MX];

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  for (int i = 0; i < MX * 2; ++i)
    for (int j = 0; j < MX * 2; ++j) {
      if (i == j || j == 0)
        cnk[i][j] = 1;
      else if (j > i)
        cnk[i][j] = 0;
      else
        cnk[i][j] = (cnk[i - 1][j] + cnk[i - 1][j - 1]) % MOD;
    }
  for (int i = 0; i < MX; ++i) {
    cc[i][0] = 1;
    for (int j = 1; j <= i; ++j) {
      cc[i][j] = (cc[i][j - 1] + cnk[i][j]) % MOD;
    }
  }
  cin >> a >> b;
  for (int i = 0; i <= a; ++i)
    for (int j = 0; j <= b; ++j) {
      if (i == 0 && j == 0)
        dp1[i][j] = 1;
      if (i)
        dp1[i][j] = dp1[i - 1][j];
      if (j) {
        dp1[i][j] = (dp1[i][j] + cc[j - 1][min(j - 1, i)]) % MOD;
      }
    }
  ll ans = 0;
  for (int i = 0; i <= a; ++i) {
    for (int j = 0; j <= b - 1; ++j) {
      if (a - i >= (b - 1 - j))
        ans = (ans + ll(cnk[b - 1][j]) * dp1[a - i - (b - 1 - j)][b - 1 - j]) %
              MOD;
    }
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long long llong;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
        ll ans = 1; while (b) {
                while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
                ans = (ans * a) % MOD, --b;
        } return ans;
}
*/
const int MX = 2010;
const ll MOD = 1e9 + 7;

int a, b;
int cnk[MX * 2][MX * 2];
int dp1[MX][MX];
int cc[MX][MX];

int main() {
#ifdef FASTIO
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  for (int i = 0; i < MX * 2; ++i)
    for (int j = 0; j < MX * 2; ++j) {
      if (i == j || j == 0)
        cnk[i][j] = 1;
      else if (j > i)
        cnk[i][j] = 0;
      else
        cnk[i][j] = (cnk[i - 1][j] + cnk[i - 1][j - 1]) % MOD;
    }
  for (int i = 0; i < MX; ++i) {
    cc[i][0] = 1;
    for (int j = 1; j <= i; ++j) {
      cc[i][j] = (cc[i][j - 1] + cnk[i][j]) % MOD;
    }
  }
  cin >> a >> b;
  for (int i = 0; i <= a; ++i)
    for (int j = 0; j <= b; ++j) {
      if (i == 0 && j == 0)
        dp1[i][j] = 1;
      if (i)
        dp1[i][j] = dp1[i - 1][j];
      if (j) {
        dp1[i][j] = (dp1[i][j] + cc[j - 1][min(j - 1, i)]) % MOD;
      }
    }
  ll ans = 0;
  for (int i = 0; i <= a; ++i) {
    for (int j = 0; j <= b - 1; ++j) {
      if (a - i >= (b - 1 - j))
        ans = (ans + ll(cnk[b - 1][j]) * dp1[a - i - (b - 1 - j)][b - 1 - j]) %
              MOD;
    }
  }
  cout << ans << "\n";
  return 0;
}