#abc273g. G - Row Column Sums 2

G - Row Column Sums 2

Score : 600600 points

问题描述

计算满足以下两个条件的 NN 阶非负整数方阵的数量,结果对 998244353998244353 取模:

  • 对于所有 i=1,2,,Ni = 1, 2, \ldots, N,第 ii 行元素之和为 RiR_i
  • 对于所有 i=1,2,,Ni = 1, 2, \ldots, N,第 ii 列元素之和为 CiC_i

注意:输入中给出的 RiR_iCiC_i 是介于 0022 之间的整数(参见约束条件)。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Find the number, modulo 998244353998244353, of square matrices of size NN whose elements are non-negative integers, that satisfy both of the following two conditions:

  • for all i=1,2,,Ni = 1, 2, \ldots, N, the sum of the elements in the ii-th row is RiR_i;
  • for all i=1,2,,Ni = 1, 2, \ldots, N, the sum of the elements in the ii-th column is CiC_i.

Note that RiR_i and CiC_i given in the input are integers between 00 and 22 (see Constraints).

Constraints

  • 1N50001 \leq N \leq 5000
  • 0Ri20 \leq R_i \leq 2
  • 0Ci20 \leq C_i \leq 2
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

R1R_1 R2R_2 \ldots RNR_N

C1C_1 C2C_2 \ldots CNC_N

Output

Print the answer.

Sample Input 1

3
1 1 1
0 1 2

Sample Output 1

3

The following 33 matrices satisfy the conditions:

0 1 0
0 0 1
0 0 1
0 0 1
0 1 0
0 0 1
0 0 1
0 0 1
0 1 0

Sample Input 2

3
1 1 1
2 2 2

Sample Output 2

0

Sample Input 3

18
2 0 1 2 0 1 1 2 1 1 2 0 1 2 2 1 0 0
1 1 0 1 1 1 1 1 1 1 1 1 2 1 1 0 2 2

Sample Output 3

968235177

Be sure to print the count modulo 998244353998244353.

update @ 2024/3/10 11:31:11