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 (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef long long int int64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

#define POS(i,j) ((i)*(n+2)+(j))

void run(void){
  char *s=(char *)calloc(2000+1,sizeof(char));
  scanf("%s",s);
  int n=strlen(s);
  const int mod=998244353;
  int *dp=(int *)calloc((n+2)*(n+2),sizeof(int));
  int i=0;
  dp[POS(1,s[i]=='2'?2:s[i]=='1'?1:0)]=1;
  for(i=1;i<n;i++){
    int d=s[i]=='2'?2:s[i]=='1'?1:0;
    for(int j=0;j<=i+1;j++){
      if(j>0){
	dp[POS(i+1,j-1+d)]+=dp[POS(i,j)];
	dp[POS(i+1,j-1+d)]%=mod;
      }
      if(j<i+1){
	dp[POS(i+1,j+d)]+=dp[POS(i,j)];
	dp[POS(i+1,j+d)]%=mod;
      }
    }
  }
  memcpy(dp,dp+POS(n,0),sizeof(int)*(n+2));
  memset(dp+POS(1,0),0,sizeof(int)*(n+2)*n);
  for(i=0;i<=n;i++){
    for(int j=0;j<=n+1-i;j++){
      if(j>0){
	dp[POS(i+1,j-1)]+=dp[POS(i,j)];
	dp[POS(i+1,j-1)]%=mod;
      }
      if(j<n+1-i){
	dp[POS(i+1,j)]+=dp[POS(i,j)];
	dp[POS(i+1,j)]%=mod;
      }
    }
  }
  printf("%d\n",dp[POS(n+1,0)]);
}

int main(void){
  run();
  return 0;
}