Permuturns

Time limit: 1s Memory limit: 256MB Input: Output:

Alin and Blin are playing a game called Palatro. They share a deck of NN cards labeled 11 through NN. Alin writes the number AiA_i on card ii, and Blin writes the number BiB_i on card ii. Each player must write every number from 11 to NN on exactly one card (thus AA and BB are permutations).

Clin is the referee of the game. He decides the order in which the two players must reveal their cards, giving NN requests of the form:
"Show the card with index C1C_1", then "Show the card with index C2C_2", and so on. Here (C1,C2,,CN)(C_1, C_2, \dots, C_N) represents a permutation of the numbers from 11 to NN.
At the beginning, Clin considers the first card shown (C1C_1) to be the winning card. Each time a new card CiC_i is revealed, Clin checks the numbers written on it: if both Alin’s number and Blin’s number are greater than those on the current winning card, then card CiC_i becomes the new winning card.

More formally, Clin uses the following algorithm to determine the winning card:

winner = C[1]
for i=2...N:
    if A[C[i]] > A[winner] and B[C[i]] > B[winner]:
        winner = C[i]

Task

For each ii from 11 to NN, determine in how many distinct ways Clin can give the requests such that the winning card is card ii. Since the answer may be very large, output it modulo 109+710^9 + 7.

Input data

The first line contains the integer NN.
The second line contains NN integers, the elements of permutation AA.
The third line contains NN integers, the elements of permutation BB.

Output data

Print NN integers, separated by spaces, the answer for each ii.

Restrictions

  • 1N200 0001 \leq N \leq 200 \ 000.
# Points Restrictions
1 6 A=1,2,...,NA = 1, 2, ..., N and B=N1,N2,...,1,NB = N-1, N-2, ..., 1, N
2 6 1N101 \leq N \leq 10
3 14 1N201 \leq N \leq 20
4 13 1N5001 \leq N \leq 500
5 16 AA and BB are generated uniformly at random
6 24 1N50001 \leq N \leq 5000
7 21 No additional restrictions

Example

stdin

3
2 1 3
3 2 1

stdout

4 0 2

Explanation

C=[1,2,3]C = [1, 2, 3] \Rightarrow At the end of the algorithm, winner=1winner = 1
C=[1,3,2]C = [1, 3, 2] \Rightarrow At the end of the algorithm, winner=1winner = 1
C=[2,1,3]C = [2, 1, 3] \Rightarrow At the end of the algorithm, winner=1winner = 1
C=[2,3,1]C = [2, 3, 1] \Rightarrow At the end of the algorithm, winner=1winner = 1
C=[3,1,2]C = [3, 1, 2] \Rightarrow At the end of the algorithm, winner=3winner = 3
C=[3,2,1]C = [3, 2, 1] \Rightarrow At the end of the algorithm, winner=3winner = 3

Log in or sign up to be able to send submissions!