In-order
The opening ceremony for the Olympic Games will take place on the river with teams on boats. The layout of the athletes on top of the boat has been designed in a very specific way: for each team, the athletes (conveniently numbered from to ) are arranged as a binary tree.
The organiser has also designed the pre-order traversal, post-order traversal, and a (possibly empty) consecutive part of the in-order traversal of the binary tree that each team must follow.
Now, to make sure there are enough tree layouts so that each team can have a distinct one, you are asked to calculate the quantity of different possible in-order traversals, say , modulo the prime number .
Input
Consists of four lines. The first line contains the number . Each subsequent line contains a list of integers.
The second line contains a list , where is the number of the -th athlete found in pre-order traversal.
The third line contains a list , where is the number of the -th athlete found in post-order traversal.
The fourth line contains a list , where is either the number of the -th athlete found in in-order traversal, or if the organiser did not say who that -th athlete should be.
Output
Print one integer : this is the only integer such that and for which is divisible by .