Problem p03134 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define EACH(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(), i##_end = (c).end();             \
       i != i##_end; ++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template <class T> inline void amin(T &x, const T &y) {
  if (y < x)
    x = y;
}
template <class T> inline void amax(T &x, const T &y) {
  if (x < y)
    x = y;
}
template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}
template <unsigned MOD> struct ModInt {
  static const unsigned static_MOD = MOD;
  unsigned x;
  void undef() { x = (unsigned)-1; }
  bool isnan() const { return x == (unsigned)-1; }
  inline int geti() const { return (int)x; }
  ModInt() { x = 0; }
  ModInt(const ModInt &y) { x = y.x; }
  ModInt(int y) {
    if (y < 0 || (int)MOD <= y)
      y %= (int)MOD;
    if (y < 0)
      y += MOD;
    x = y;
  }
  ModInt(unsigned y) {
    if (MOD <= y)
      x = y % MOD;
    else
      x = y;
  }
  ModInt(long long y) {
    if (y < 0 || MOD <= y)
      y %= MOD;
    if (y < 0)
      y += MOD;
    x = y;
  }
  ModInt(unsigned long long y) {
    if (MOD <= y)
      x = y % MOD;
    else
      x = y;
  }
  ModInt &operator+=(const ModInt y) {
    if ((x += y.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt y) {
    if ((x -= y.x) & (1u << 31))
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt y) {
    x = (unsigned long long)x * y.x % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt y) {
    x = (unsigned long long)x * y.inv().x % MOD;
    return *this;
  }
  ModInt operator-() const { return (x ? MOD - x : 0); }

  ModInt inv() const {
    unsigned a = MOD, b = x;
    int u = 0, v = 1;
    while (b) {
      int t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    if (u < 0)
      u += MOD;
    return ModInt(u);
  }
  ModInt pow(long long y) const {
    ModInt b = *this, r = 1;
    if (y < 0) {
      b = b.inv();
      y = -y;
    }
    for (; y; y >>= 1) {
      if (y & 1)
        r *= b;
      b *= b;
    }
    return r;
  }
  friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
  friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
  friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
  friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
  friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
  friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
  friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};

const LL MOD = 998244353;
typedef ModInt<MOD> Mint;

int N;
char S[2011];

Mint dp[2011][2011];

void MAIN() {
  scanf("%s", S);
  N = strlen(S);

  dp[0][0] = 1;
  int R = 0, B = 0;
  REP(i, N * 2) {
    int adR = 0, adB = 0;
    if (i < N) {
      if (S[i] == '0')
        adR += 2;
      if (S[i] == '1') {
        adR++;
        adB++;
      }
      if (S[i] == '2')
        adB += 2;
    }

    REP(r, R + 1) {
      int b = R + B - i - r;
      if (b < 0 || B < b)
        continue;
      if (r + adR)
        dp[i + 1][r + adR - 1] += dp[i][r];
      if (b + adB)
        dp[i + 1][r + adR] += dp[i][r];
    }
    R += adR;
    B += adB;
  }

  printf("%d\n", dp[N * 2][0].geti());
}

int main() {
  int TC = 1;
  //    scanf("%d", &TC);
  REP(tc, TC) MAIN();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define EACH(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(), i##_end = (c).end();             \
       i != i##_end; ++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template <class T> inline void amin(T &x, const T &y) {
  if (y < x)
    x = y;
}
template <class T> inline void amax(T &x, const T &y) {
  if (x < y)
    x = y;
}
template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}
template <unsigned MOD> struct ModInt {
  static const unsigned static_MOD = MOD;
  unsigned x;
  void undef() { x = (unsigned)-1; }
  bool isnan() const { return x == (unsigned)-1; }
  inline int geti() const { return (int)x; }
  ModInt() { x = 0; }
  ModInt(const ModInt &y) { x = y.x; }
  ModInt(int y) {
    if (y < 0 || (int)MOD <= y)
      y %= (int)MOD;
    if (y < 0)
      y += MOD;
    x = y;
  }
  ModInt(unsigned y) {
    if (MOD <= y)
      x = y % MOD;
    else
      x = y;
  }
  ModInt(long long y) {
    if (y < 0 || MOD <= y)
      y %= MOD;
    if (y < 0)
      y += MOD;
    x = y;
  }
  ModInt(unsigned long long y) {
    if (MOD <= y)
      x = y % MOD;
    else
      x = y;
  }
  ModInt &operator+=(const ModInt y) {
    if ((x += y.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt y) {
    if ((x -= y.x) & (1u << 31))
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt y) {
    x = (unsigned long long)x * y.x % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt y) {
    x = (unsigned long long)x * y.inv().x % MOD;
    return *this;
  }
  ModInt operator-() const { return (x ? MOD - x : 0); }

  ModInt inv() const {
    unsigned a = MOD, b = x;
    int u = 0, v = 1;
    while (b) {
      int t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    if (u < 0)
      u += MOD;
    return ModInt(u);
  }
  ModInt pow(long long y) const {
    ModInt b = *this, r = 1;
    if (y < 0) {
      b = b.inv();
      y = -y;
    }
    for (; y; y >>= 1) {
      if (y & 1)
        r *= b;
      b *= b;
    }
    return r;
  }
  friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
  friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
  friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
  friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
  friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
  friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
  friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};

const LL MOD = 998244353;
typedef ModInt<MOD> Mint;

int N;
char S[2011];

Mint dp[4011][4011];

void MAIN() {
  scanf("%s", S);
  N = strlen(S);

  dp[0][0] = 1;
  int R = 0, B = 0;
  REP(i, N * 2) {
    int adR = 0, adB = 0;
    if (i < N) {
      if (S[i] == '0')
        adR += 2;
      if (S[i] == '1') {
        adR++;
        adB++;
      }
      if (S[i] == '2')
        adB += 2;
    }

    REP(r, R + 1) {
      int b = R + B - i - r;
      if (b < 0 || B < b)
        continue;
      if (r + adR)
        dp[i + 1][r + adR - 1] += dp[i][r];
      if (b + adB)
        dp[i + 1][r + adR] += dp[i][r];
    }
    R += adR;
    B += adB;
  }

  printf("%d\n", dp[N * 2][0].geti());
}

int main() {
  int TC = 1;
  //    scanf("%d", &TC);
  REP(tc, TC) MAIN();
  return 0;
}