Order-Preserving Partition
시간 제한2초메모리 제한512 MB
순열을 네 개의 연속된 비어 있지 않은 구간으로 나눌 때, 각 구간의 값이 연속 정수가 되고 구간 최솟값의 순서가 주어진 순위 순열과 일치하는 분할의 수를 센다.
문제
Bobo has two permutations: and . He would like to partition 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 , where is the minimum value in the -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 .
입력
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 , the length of the first permutation .
The second line contains integers .
The third line contains four integers .
It is guaranteed that the sum of all does not exceed .
출력
For each test case, output an integer denoting the answer.