Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 998244353
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \
  cout << #a << ":";                                                           \
  rep(pachico, n) cout << " " << a[pachico];                                   \
  cout << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(pachico, v.size()) cout << " " << v[pachico];                            \
  cout << endl
#define svecp(v)                                                               \
  cout << #v << ":";                                                           \
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(pachico, s) cout << " " << pachico;                                     \
  cout << endl
#define smap(m)                                                                \
  cout << #m << ":";                                                           \
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 2005;

inline int add(int x, int y) { return (x + y) % MOD; }

inline int sub(int x, int y) { return (x + MOD - y) % MOD; }

inline int mul(int x, int y) { return (ll)x * y % MOD; }

int dp[MAX_N][MAX_N], ok[MAX_N][MAX_N];
int tma[2 * MAX_N], tmb[2 * MAX_N];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = len(s);
  dp[0][0] = 1;
  vi apos, bpos;
  rep(i, n) {
    if (s[i] == '0') {
      apos.pb(i + 1), apos.pb(i + 1);
    } else if (s[i] == '1') {
      apos.pb(i + 1), bpos.pb(i + 1);
    } else {
      bpos.pb(i + 1), bpos.pb(i + 1);
    }
  }
  rep(i, len(apos) + 1) {
    rep(j, len(bpos) + 1) { ok[i][j] = INF; }
  }
  ok[0][0] = 0;
  rep(i, len(apos) + 1) {
    rep(j, len(bpos) + 1) {
      if (i > 0) {
        cmn(ok[i][j], max(ok[i - 1][j] + 1, apos[i - 1]));
      }
      if (j > 0) {
        cmn(ok[i][j], max(ok[i][j - 1] + 1, bpos[j - 1]));
      }
    }
  }
  rep(i, len(apos) + 1) {
    rep(j, len(bpos) + 1) {
      if (i < len(apos) && ok[i + 1][j] <= i + j + 1) {
        dp[i + 1][j] = add(dp[i + 1][j], dp[i][j]);
      }
      if (j < len(bpos) && ok[i][j + 1] <= i + j + 1) {
        dp[i][j + 1] = add(dp[i][j + 1], dp[i][j]);
      }
    }
  }
  int ans = 0;
  rep(i, 2 * n + 1) { ans = add(ans, dp[i][2 * n - i]); }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 998244353
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \
  cout << #a << ":";                                                           \
  rep(pachico, n) cout << " " << a[pachico];                                   \
  cout << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(pachico, v.size()) cout << " " << v[pachico];                            \
  cout << endl
#define svecp(v)                                                               \
  cout << #v << ":";                                                           \
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(pachico, s) cout << " " << pachico;                                     \
  cout << endl
#define smap(m)                                                                \
  cout << #m << ":";                                                           \
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \
                        << "}";                                                \
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 4005;

inline int add(int x, int y) { return (x + y) % MOD; }

inline int sub(int x, int y) { return (x + MOD - y) % MOD; }

inline int mul(int x, int y) { return (ll)x * y % MOD; }

int dp[MAX_N][MAX_N], ok[MAX_N][MAX_N];
int tma[2 * MAX_N], tmb[2 * MAX_N];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = len(s);
  dp[0][0] = 1;
  vi apos, bpos;
  rep(i, n) {
    if (s[i] == '0') {
      apos.pb(i + 1), apos.pb(i + 1);
    } else if (s[i] == '1') {
      apos.pb(i + 1), bpos.pb(i + 1);
    } else {
      bpos.pb(i + 1), bpos.pb(i + 1);
    }
  }
  rep(i, len(apos) + 1) {
    rep(j, len(bpos) + 1) { ok[i][j] = INF; }
  }
  ok[0][0] = 0;
  rep(i, len(apos) + 1) {
    rep(j, len(bpos) + 1) {
      if (i > 0) {
        cmn(ok[i][j], max(ok[i - 1][j] + 1, apos[i - 1]));
      }
      if (j > 0) {
        cmn(ok[i][j], max(ok[i][j - 1] + 1, bpos[j - 1]));
      }
    }
  }
  rep(i, len(apos) + 1) {
    rep(j, len(bpos) + 1) {
      if (i < len(apos) && ok[i + 1][j] <= i + j + 1) {
        dp[i + 1][j] = add(dp[i + 1][j], dp[i][j]);
      }
      if (j < len(bpos) && ok[i][j + 1] <= i + j + 1) {
        dp[i][j + 1] = add(dp[i][j + 1], dp[i][j]);
      }
    }
  }
  int ans = 0;
  rep(i, 2 * n + 1) { ans = add(ans, dp[i][2 * n - i]); }
  cout << ans << "\n";
  return 0;
}