Problem p03134 (C++)

Buggy submission (status: Runtime Error):
/**********************************************************************/
/********                                                      ********/
/********                     Writer:孙~~                      ********/
/********                       Lang:C++                       ********/
/********                     Status:Unknow                    ********/
/********                                                      ********/
/**********************************************************************/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int SIZE = 2005;
const double EPS = 1e-6;
const int MOD = 998244353;

long long dp[SIZE][SIZE], c[SIZE][SIZE];
int r[SIZE], b[SIZE];

int main() {
  /*main function start*/
  string s;
  cin >> s;
  int n = s.size();
  //	cout<<n<<endl;
  for (int i = 1; i <= n; i++)
    if (s[i - 1] == '0')
      r[i] = r[i - 1] + 2, b[i] = b[i - 1];
    else if (s[i - 1] == '1')
      r[i] = r[i - 1] + 1, b[i] = b[i - 1] + 1;
    else
      r[i] = r[i - 1], b[i] = b[i - 1] + 2;
  for (int i = n + 1; i <= n * 2; i++)
    r[i] = r[i - 1], b[i] = b[i - 1];
  dp[0][0] = 1;
  for (int i = 1; i <= n * 2; i++) {
    //		cout<<"TEST "<<i<<" "<<b[i]<<" "<<max(i-b[i],0)<<"
    //"<<min(i,r[i])<<endl;
    for (int j = max(i - b[i], 0); j <= min(i, r[i]); j++) {
      if (j > 0)
        dp[i][j] += dp[i - 1][j - 1];
      if (i != j)
        dp[i][j] += dp[i - 1][j];
      dp[i][j] %= MOD;
      //			cout<<dp[i][j]<<" ";
    }
    //		cout<<endl;
  }
  long long ans = 0;
  for (int i = 0; i <= min(n * 2, r[n * 2]); i++) {
    ans += dp[n * 2][i];
    ans %= MOD;
  }
  cout << ans << endl;
  //	cout<<c[2][0]<<endl;
  return 0;
}


Fixed submission:
/**********************************************************************/
/********                                                      ********/
/********                     Writer:孙~~                      ********/
/********                       Lang:C++                       ********/
/********                     Status:Unknow                    ********/
/********                                                      ********/
/**********************************************************************/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int SIZE = 2005;
const double EPS = 1e-6;
const int MOD = 998244353;

long long dp[SIZE * 2][SIZE * 2];
int r[SIZE * 2], b[SIZE * 2];

int main() {
  /*main function start*/
  string s;
  cin >> s;
  int n = s.size();
  //	cout<<n<<endl;
  for (int i = 1; i <= n; i++)
    if (s[i - 1] == '0')
      r[i] = r[i - 1] + 2, b[i] = b[i - 1];
    else if (s[i - 1] == '1')
      r[i] = r[i - 1] + 1, b[i] = b[i - 1] + 1;
    else
      r[i] = r[i - 1], b[i] = b[i - 1] + 2;
  for (int i = n + 1; i <= n * 2; i++)
    r[i] = r[i - 1], b[i] = b[i - 1];
  dp[0][0] = 1;
  for (int i = 1; i <= n * 2; i++) {
    //		cout<<"TEST "<<i<<" "<<b[i]<<" "<<max(i-b[i],0)<<"
    //"<<min(i,r[i])<<endl;
    for (int j = max(i - b[i], 0); j <= min(i, r[i]); j++) {
      if (j > 0)
        dp[i][j] += dp[i - 1][j - 1];
      if (i != j)
        dp[i][j] += dp[i - 1][j];
      dp[i][j] %= MOD;
      //			cout<<dp[i][j]<<" ";
    }
    //		cout<<endl;
  }
  long long ans = 0;
  for (int i = 0; i <= min(n * 2, r[n * 2]); i++) {
    ans += dp[n * 2][i];
    ans %= MOD;
  }
  cout << ans << endl;
  //	cout<<c[2][0]<<endl;
  return 0;
}