luogu#P8180. 「EZEC-11」Unmemorable
「EZEC-11」Unmemorable
Problem Description
Unputdownable has a permutation of length .
While practicing monotonic stacks, he used a program to compute, for each , the largest such that and , and the smallest such that and .
In particular, if such an does not exist, it is defined as , and if such an does not exist, it is defined as .
One day, Unputdownable forgot the permutation , and only the arrays and after being rearranged separately were left. Can you help him recover the original permutation ?
Later, since he found that cannot be recovered, you only need to tell him how many possible original permutations there are.
Output the answer modulo . The testdata guarantees that at least one solution exists.
Input Format
The first line contains an integer .
The second line contains integers, representing the rearranged .
The third line contains integers, representing the rearranged .
Output Format
Output one line containing one integer, the number of possible permutations modulo .
5
3 1 0 0 4
6 3 6 3 6
6
Hint
Sample Explanation 1
One possible permutation is . The array is , and the array is .
This problem uses bundled tests.
- Subtask 1 (5 pts): .
- Subtask 2 (15 pts): .
- Subtask 3 (15 pts): , and it is guaranteed that there exists a permutation such that the computed from are exactly the given ones.
- Subtask 4 (25 pts): , and it is guaranteed that there exists a permutation such that the computed from are exactly the given ones.
- Subtask 5 (40 pts): No special constraints.
For of the testdata, , . The testdata guarantees that at least one solution exists.
Translated by ChatGPT 5