Problem:
Score : 900 points

Problem Statement

There are N Snukes lining up in a row.
You are given a string S of length N. The i-th Snuke from the front has two red balls if the i-th character in S is 0; one red ball and one blue ball if the i-th character in S is 1; two blue balls if the i-th character in S is 2.

Takahashi has a sequence that is initially empty. Find the number of the possible sequences he may have after repeating the following procedure 2N times, modulo 998244353:

Each Snuke who has one or more balls simultaneously chooses one of his balls and hand it to the Snuke in front of him, or hand it to Takahashi if he is the first Snuke in the row.

Takahashi receives the ball and put it to the end of his sequence.

Constraints

1 \leq |S| \leq 2000

S consists of 0,1 and 2.

Note that the integer N is not directly given in input; it is given indirectly as the length of the string S.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of the possible sequences Takahashi may have after repeating the procedure 2N times, modulo 998244353.

Sample Input 1

02

Sample Output 1

3

There are three sequences that Takahashi may have: rrbb, rbrb and rbbr, where r and b stand for red and blue balls, respectively.

Sample Input 2

1210

Sample Output 2

55

Sample Input 3

12001021211100201020

Sample Output 3

543589959

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <functional>


const int MOD = 998244353;
const int kN = 4000 + 5;
int f[2][kN];
char s[kN];
int n;
int most_ball[2][kN];

void add(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}

void calc_most_ball(int* most_ball, const std::function<int(char)>& func) {
  for (int i = 0, sum = 0; i < n + n; ++ i) {
    if (i < n) {
      sum += func(s[i]);
    }
    most_ball[i + 1] = std::min(i + 1, sum);
  }
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  calc_most_ball(most_ball[0], [](char x) { return x - '0'; });
  calc_most_ball(most_ball[1], [](char x) { return 2 - (x - '0'); });
  f[0][0] = 1;
  for (int i = 1; i <= n + n; ++ i) {
    int cur = i & 1;
    int pre = cur ^ 1;
    memset(f[cur], 0, sizeof(f[cur]));
    for (int j = 0; j <= most_ball[0][i - 1]; ++ j) if (f[pre][j]) {
      if (j + 1 <= most_ball[0][i])
        add(f[cur][j + 1], f[pre][j]);
      if (i - j <= most_ball[1][i])
        add(f[cur][j], f[pre][j]);
    }
  }
  int result = 0;
  for (int i = 0; i <= most_ball[0][n + n]; ++ i)
    add(result, f[0][i]);
  printf("%d\n", result);
}