Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long ll;

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll kai(ll x, ll y, ll m) {
  ll res = 1;
  for (ll i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= m;
  }
  return res;
}
ll mod_pow(ll x, ll y, ll m) {
  ll res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

ll comb(ll x, ll y, ll m) {
  if (y > x)
    return 0;
  return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

const ll mod = 998244353;

string s;
ll cum1[2010], cum2[2010];
ll d[2010][2010];

signed main() {
  cin >> s;
  int n = (int)s.size();
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      cum1[i + 1] += 2;
    if (s[i] == '1')
      cum1[i + 1]++, cum2[i + 1]++;
    if (s[i] == '2')
      cum2[i + 1] += 2;
    cum1[i + 1] += cum1[i];
    cum2[i + 1] += cum2[i];
  }
  d[0][0] = 1;
  for (int i = 0; i < 2 * n; i++) {
    for (int j = 0; j <= i; j++) {
      if (j + 1 <= cum1[min(n, i + 1)])
        d[j + 1][i - j] = (d[j + 1][i - j] + d[j][i - j]) % mod;
      if (i - j + 1 <= cum2[min(n, i + 1)])
        d[j][i + 1 - j] = (d[j][i + 1 - j] + d[j][i - j]) % mod;
    }
  }
  cout << d[cum1[n]][cum2[n]] << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long ll;

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll kai(ll x, ll y, ll m) {
  ll res = 1;
  for (ll i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= m;
  }
  return res;
}
ll mod_pow(ll x, ll y, ll m) {
  ll res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

ll comb(ll x, ll y, ll m) {
  if (y > x)
    return 0;
  return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

const ll mod = 998244353;

string s;
ll cum1[2010], cum2[2010];
ll d[4010][4010];

signed main() {
  cin >> s;
  int n = (int)s.size();
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      cum1[i + 1] += 2;
    if (s[i] == '1')
      cum1[i + 1]++, cum2[i + 1]++;
    if (s[i] == '2')
      cum2[i + 1] += 2;
    cum1[i + 1] += cum1[i];
    cum2[i + 1] += cum2[i];
  }
  d[0][0] = 1;
  for (int i = 0; i < 2 * n; i++) {
    for (int j = 0; j <= i; j++) {
      if (j + 1 <= cum1[min(n, i + 1)])
        d[j + 1][i - j] = (d[j + 1][i - j] + d[j][i - j]) % mod;
      if (i - j + 1 <= cum2[min(n, i + 1)])
        d[j][i + 1 - j] = (d[j][i + 1 - j] + d[j][i - j]) % mod;
    }
  }
  cout << d[cum1[n]][cum2[n]] << endl;
}