Order-Preserving Partition

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

Bobo has two permutations: P=p_1,p_2,,p_nP = \\{p\_1, p\_2, \ldots, p\_n\\} and Q=q_1,q_2,q_3,q_4Q = \\{q\_1, q\_2, q\_3, q\_4\\}. He would like to partition PP into four non-empty and contiguous parts in such a manner that:

  • The numbers in each part can be rearranged to form an interval of values: an increasing sequence where each element is greater than the previous by exactly one.
  • For all 1i<j41 \leq i < j \leq 4, (s_is_j)(q_iq_j)>0(s\_i - s\_j) \cdot (q\_i - q\_j) > 0 where s_is\_i is the minimum value in the ii-th part.

Bobo wants to know the number of such partitions. As the number may be very large, you just need to print the answer modulo (109+7)(10^9 + 7).

입력

The input contains zero or more test cases, and is terminated by end-of-file. For each test case:

The first line contains an integer nn, the length of the first permutation (4n106)(4 \leq n \leq 10^6).

The second line contains nn integers p_1,p_2,,p_np\_1, p\_2, \dots, p\_n.

The third line contains four integers q_1,q_2,q_3,q_4q\_1, q\_2, q\_3, q\_4.

It is guaranteed that the sum of all nn does not exceed 10610^6.

출력

For each test case, output an integer denoting the answer.