Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l + r) >> 1)
#define MAXN 2005
#define MAXM
#define mod 998244353
#define inf (1 << 30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for (register int i = x; i <= (y); ++i)
#define repd(i, x, y) for (register int i = x; i >= (y); --i)
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) { return x > y ? x = y, 1 : 0; }
template <typename T> il bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; }
template <typename T> il void read(T &x) {
  char ch = getchar();
  int f = 1;
  x = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
  read(x), read(args...);
}
int n;
char s[MAXN];
ll dp[MAXN][MAXN];
void add(ll &x, ll y) { (x += y) %= mod; }
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  dp[0][0] = 1;
  int r = 0, b = 0;
  rep(i, 1, n * 2) {
    if (i <= n)
      b += s[i] - '0', r += '2' - s[i];
    rep(j, 0, i) if (r >= j && b >= i - j) {
      add(dp[i][j], dp[i - 1][j]);
      if (j)
        add(dp[i][j], dp[i - 1][j - 1]);
    }
  }
  cout << dp[n * 2][r] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l + r) >> 1)
#define MAXN 4005
#define MAXM
#define mod 998244353
#define inf (1 << 30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for (register int i = x; i <= (y); ++i)
#define repd(i, x, y) for (register int i = x; i >= (y); --i)
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) { return x > y ? x = y, 1 : 0; }
template <typename T> il bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; }
template <typename T> il void read(T &x) {
  char ch = getchar();
  int f = 1;
  x = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
  read(x), read(args...);
}
int n;
char s[MAXN];
ll dp[MAXN][MAXN];
void add(ll &x, ll y) { (x += y) %= mod; }
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  dp[0][0] = 1;
  int r = 0, b = 0;
  rep(i, 1, n * 2) {
    if (i <= n)
      b += s[i] - '0', r += '2' - s[i];
    rep(j, 0, i) if (r >= j && b >= i - j) {
      add(dp[i][j], dp[i - 1][j]);
      if (j)
        add(dp[i][j], dp[i - 1][j - 1]);
    }
  }
  cout << dp[n * 2][r] << endl;
  return 0;
}