Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
namespace io {
int F() {
  int F = 1, n = 0;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
long long G() {
  long long F = 1, n = 0;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
} // namespace io
int R(int l, int r) { return (rand() << 15 | rand()) % (r - l + 1) + l; }
const int M = 998244353;
char s[3333];
int b[2222][2222], f[2222][2222];
int sub[5][2] = {{'3', 0}, {'4', '3'}, {0, '4'}};
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  int R = 0, B = 0;
  for (register int i = 1; i <= n; ++i) {
    if (s[i] == '0')
      R += 2;
    if (s[i] == '1')
      ++R, ++B;
    if (s[i] == '2')
      B += 2;
  }
  int x = 0, y = 0;
  while (1) {
    int pl = 0;
    for (register int i = 1; i <= n; ++i)
      if (s[i] == '0' || s[i] == '2') {
        pl = i;
        break;
      }
    if (pl == 0)
      break;
    if (s[pl] == '0') {
      b[x][y + pl] = 1;
      s[1] = sub[s[1] - '0'][0];
      ++x;
    } else {
      b[x + pl][y] = 1;
      s[1] = sub[s[1] - '0'][1];
      ++y;
    }
    for (register int i = 1; i < n; ++i) {
      if (s[i] == '3') {
        if (s[i + 1] != '0' && s[i + 1] != '3')
          s[i] = '1', s[i + 1] = sub[s[i + 1] - '0'][1];
        else
          s[i] = '0', s[i + 1] = sub[s[i + 1] - '0'][0];
      } else {
        if (s[i + 1] != '2' && s[i + 1] != '4')
          s[i] = '1', s[i + 1] = sub[s[i + 1] - '0'][0];
        else
          s[i] = '2', s[i + 1] = sub[s[i + 1] - '0'][1];
      }
    }
    if (!s[n])
      --n;
  }
  f[0][0] = 1;
  for (register int i = 0; i <= R; ++i)
    for (register int j = 0; j <= B; ++j)
      if (b[i][j] == 0) {
        if (i)
          f[i][j] = (f[i][j] + f[i - 1][j]) % M;
        if (j)
          f[i][j] = (f[i][j] + f[i][j - 1]) % M;
      }
  printf("%d\n", f[R][B]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define cmin(a, b) (a > (b) ? a = (b), 1 : 0)
#define cmax(a, b) (a < (b) ? a = (b), 1 : 0)
#define dmin(a, b) ((a) < (b) ? (a) : (b))
#define dmax(a, b) ((a) > (b) ? (a) : (b))
namespace io {
int F() {
  int F = 1, n = 0;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
long long G() {
  long long F = 1, n = 0;
  char ch;
  while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
    ;
  ch == '-' ? F = 0 : n = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    n = n * 10 + ch - '0';
  return F ? n : -n;
}
} // namespace io
int R(int l, int r) { return (rand() << 15 | rand()) % (r - l + 1) + l; }
const int M = 998244353;
char s[3333];
int b[4444][4444], f[4444][4444];
int sub[5][2] = {{'3', 0}, {'4', '3'}, {0, '4'}};
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  int R = 0, B = 0;
  for (register int i = 1; i <= n; ++i) {
    if (s[i] == '0')
      R += 2;
    if (s[i] == '1')
      ++R, ++B;
    if (s[i] == '2')
      B += 2;
  }
  int x = 0, y = 0;
  while (1) {
    int pl = 0;
    for (register int i = 1; i <= n; ++i)
      if (s[i] == '0' || s[i] == '2') {
        pl = i;
        break;
      }
    if (pl == 0)
      break;
    if (s[pl] == '0') {
      b[x][y + pl] = 1;
      s[1] = sub[s[1] - '0'][0];
      ++x;
    } else {
      b[x + pl][y] = 1;
      s[1] = sub[s[1] - '0'][1];
      ++y;
    }
    for (register int i = 1; i < n; ++i) {
      if (s[i] == '3') {
        if (s[i + 1] != '0' && s[i + 1] != '3')
          s[i] = '1', s[i + 1] = sub[s[i + 1] - '0'][1];
        else
          s[i] = '0', s[i + 1] = sub[s[i + 1] - '0'][0];
      } else {
        if (s[i + 1] != '2' && s[i + 1] != '4')
          s[i] = '1', s[i + 1] = sub[s[i + 1] - '0'][0];
        else
          s[i] = '2', s[i + 1] = sub[s[i + 1] - '0'][1];
      }
    }
    if (!s[n])
      --n;
  }
  f[0][0] = 1;
  for (register int i = 0; i <= R; ++i)
    for (register int j = 0; j <= B; ++j)
      if (b[i][j] == 0) {
        if (i)
          f[i][j] = (f[i][j] + f[i - 1][j]) % M;
        if (j)
          f[i][j] = (f[i][j] + f[i][j - 1]) % M;
      }
  printf("%d\n", f[R][B]);
  return 0;
}