atcoder#ARC127E. [ARC127E] Priority Queue

[ARC127E] Priority Queue

Score : 800800 points

Problem Statement

Given is a sequence of A+BA+B integers (X1,X2,,XA+B)(X_1,X_2,\cdots,X_{A+B}), which contains exactly AA ones and exactly BB twos.

Snuke has a set ss, which is initially empty. He is going to do A+BA+B operations. The ii-th operation is as follows.

  • If Xi=1X_i=1: Choose an integer vv such that 1vA1 \leq v \leq A and add it to ss. Here, vv must not be an integer that was chosen in a previous operation.
  • If Xi=2X_i=2: Delete from ss the element with the largest value. The input guarantees that ss is not empty just before this operation.

How many sets are there that can be the final state of ss? Find the count modulo 998244353998244353.

Constraints

  • 1A50001 \leq A \leq 5000
  • 0BA10 \leq B \leq A-1
  • 1Xi21 \leq X_i \leq 2
  • Xi=1X_i=1 for exactly AA indices ii.
  • Xi=2X_i=2 for exactly BB indices ii.
  • ss will not be empty just before an operation with Xi=2X_i=2.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

AA BB

X1X_1 X2X_2 \cdots XA+BX_{A+B}

Output

Print the answer modulo 998244353998244353.

3 1
1 1 2 1
2

There are two possible final states of ss: s={1,2},{1,3}s=\{1,2\},\{1,3\}.

For example, the following sequence of operations results in s={1,3}s=\{1,3\}.

  • i=1i=1: Add 22 to ss.
  • i=2i=2: Add 11 to ss.
  • i=3i=3: Delete 22 from ss.
  • i=4i=4: Add 33 to ss.
20 6
1 1 1 1 1 2 1 1 1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1
5507