Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template <typename A, typename B> inline char SMAX(A &a, const B &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename A, typename B> inline char SMIN(A &a, const B &b) {
  return b < a ? a = b, 1 : 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> pii;

template <typename I> inline void read(I &x) {
  int f = 0, c;
  while (!isdigit(c = getchar()))
    c == '-' ? f = 1 : 0;
  x = c & 15;
  while (isdigit(c = getchar()))
    x = (x << 1) + (x << 3) + (c & 15);
  f ? x = -x : 0;
}

const int N = 1000 + 7;
const int P = 998244353;

int n, cntb[N << 1], dp[N << 1][N << 1];
char s[N];

inline void Inc(int &x, int y) {
  x += y;
  x >= P ? x -= P : 0;
}

int main() {
#ifdef hzhkk
  freopen("hkk.in", "r", stdin);
#endif
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; ++i)
    cntb[i] = cntb[i - 1] + s[i] - '0';
  for (int i = n + 1; i <= (n << 1); ++i)
    cntb[i] = cntb[i - 1];
  dp[0][0] = 1;
  for (int i = 1; i <= (n << 1); ++i) {
    for (int j = std::max(cntb[i] - i, 0); j <= std::min(cntb[i], i); ++j) {
      if (j)
        dp[i][j] = dp[i - 1][j - 1];
      if (i > j)
        Inc(dp[i][j], dp[i - 1][j]);
      //  dbg("dp[%d][%d] = %d\n", i, j, dp[i][j]);
    }
  }
  printf("%d\n", dp[n << 1][cntb[n]]);
}

Fixed submission:
#include <bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template <typename A, typename B> inline char SMAX(A &a, const B &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename A, typename B> inline char SMIN(A &a, const B &b) {
  return b < a ? a = b, 1 : 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> pii;

template <typename I> inline void read(I &x) {
  int f = 0, c;
  while (!isdigit(c = getchar()))
    c == '-' ? f = 1 : 0;
  x = c & 15;
  while (isdigit(c = getchar()))
    x = (x << 1) + (x << 3) + (c & 15);
  f ? x = -x : 0;
}

const int N = 2000 + 7;
const int P = 998244353;

int n, cntb[N << 1], dp[N << 1][N << 1];
char s[N];

inline void Inc(int &x, int y) {
  x += y;
  x >= P ? x -= P : 0;
}

int main() {
#ifdef hzhkk
  freopen("hkk.in", "r", stdin);
#endif
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; ++i)
    cntb[i] = cntb[i - 1] + s[i] - '0';
  for (int i = n + 1; i <= (n << 1); ++i)
    cntb[i] = cntb[i - 1];
  dp[0][0] = 1;
  for (int i = 1; i <= (n << 1); ++i) {
    for (int j = std::max(cntb[i] - i, 0); j <= std::min(cntb[i], i); ++j) {
      if (j)
        dp[i][j] = dp[i - 1][j - 1];
      if (i > j)
        Inc(dp[i][j], dp[i - 1][j]);
      //  dbg("dp[%d][%d] = %d\n", i, j, dp[i][j]);
    }
  }
  printf("%d\n", dp[n << 1][cntb[n]]);
}