Yet Another Contest 9 P4 - Grid Push

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 3.0s
Memory limit: 512M

Author:
Problem types

You have a N×N grid of integers G which is initially filled with 0s. You are also given arrays A and B of length N. You can perform the following two operations any number of times and in any order on the grid:

  1. Push A onto the first column of G. All columns move to the right by one, with the last column being deleted. Formally, if G is the grid after the operation is completed, then for all 1rN, Gr,1=Ar and for all 2cN, Gr,c=Gr,c1.
  2. Push B onto the first row of G. All rows move down by one, with the last row being deleted. Formally, if G is the grid after the operation is completed, then for all 1cN, G1,c=Bc and for all 2rN, Gr,c=Gr1,c.

What is the number of distinct grids with no 0s that can be created? Since the answer may be very large, output it modulo 109+7.

Constraints

1N106

1Ai,Bi2N

Subtask 1 [30%]

1N2000

Ai=1 and Bi=2

Subtask 2 [40%]

1N2000

Subtask 3 [30%]

No additional constraints.

Input Specification

The first line contains a single integer, N.

The second line will contain N space-separated integers, A1,A2,,AN.

The third line will contain N space-separated integers, B1,B2,,BN.

Output Specification

Output a single integer representing the number of distinct grids that can be created modulo 109+7.

Sample Input 1

Copy
1
1
1

Sample Output 1

Copy
1

Sample Input 2

Copy
1
1
2

Sample Output 2

Copy
2

Sample Input 3

Copy
2
1 1
1 2

Sample Output 3

Copy
3

Comments

There are no comments at the moment.