102736: [AtCoder]ABC273 G - Row Column Sums 2

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

Problem Statement

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

  • for all $i = 1, 2, \ldots, N$, the sum of the elements in the $i$-th row is $R_i$;
  • for all $i = 1, 2, \ldots, N$, the sum of the elements in the $i$-th column is $C_i$.

Note that $R_i$ and $C_i$ given in the input are integers between $0$ and $2$ (see Constraints).

Constraints

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

Input

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

$N$
$R_1$ $R_2$ $\ldots$ $R_N$
$C_1$ $C_2$ $\ldots$ $C_N$

Output

Print the answer.


Sample Input 1

3
1 1 1
0 1 2

Sample Output 1

3

The following $3$ 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 $998244353$.

Input

题意翻译

给定正整数$N$,求有多少个 $N\times N$ 的矩阵满足以下条件: - 对于所有 $1\leq i\leq N$,第 i 行上所有数之和为 $R_i$ 。 - 对于所有 $1\leq i\leq N$,第 i 列上所有数之和为 $C_i$ 。 $C_i$ , $R_i$ 都是 $0$ 到 $2$ 之间的正整数。 答案对 998244353 取模。

Output

分数:600分 部分 问题描述 找出满足以下两个条件的N阶方阵(元素为非负整数)的数量(对998244353取模): 对于所有$i=1,2,\ldots,N$,第i行元素之和为$R_i$; 对于所有$i=1,2,\ldots,N$,第i列元素之和为$C_i$。 注意,输入中给出的$R_i$和$C_i$是介于0和2之间的整数(参见约束条件)。 部分 约束条件 $1\leq N\leq 5000$ $0\leq R_i\leq 2$ $0\leq C_i\leq 2$ 输入中的所有值都是整数。 部分 输入 输入从标准输入中以以下格式给出: $N$ $R_1$ $R_2$ $\ldots$ $R_N$ $C_1$ $C_2$ $\ldots$ $C_N$ 部分 输出 打印答案。 部分 样例输入1 3 1 1 1 0 1 2 部分 样例输出1 3 以下3个矩阵满足条件: 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 部分 样例输入2 3 1 1 1 2 2 2 部分 样例输出2 0 部分 样例输入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 部分 样例输出3 968235177 务必对计数取模$998244353$。

加入题单

算法标签: