You have a grid of integers
which is initially filled with
s. You are also given arrays
and
of length
. You can perform the following two operations any number of times and in any order on the grid:
- Push
onto the first column of
. All columns move to the right by one, with the last column being deleted. Formally, if
is the grid after the operation is completed, then for all
,
and for all
,
.
- Push
onto the first row of
. All rows move down by one, with the last row being deleted. Formally, if
is the grid after the operation is completed, then for all
,
and for all
,
.
What is the number of distinct grids with no s that can be created? Since the answer may be very large, output it modulo
.
Constraints
Subtask 1 [30%]
and
Subtask 2 [40%]
Subtask 3 [30%]
No additional constraints.
Input Specification
The first line contains a single integer, .
The second line will contain space-separated integers,
.
The third line will contain space-separated integers,
.
Output Specification
Output a single integer representing the number of distinct grids that can be created modulo .
Sample Input 1
1
1
1
Sample Output 1
1
Sample Input 2
1
1
2
Sample Output 2
2
Sample Input 3
2
1 1
1 2
Sample Output 3
3
Comments