In-order#11757

In-order

1 second
128 megabytes
Very hard

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 a_1, a_2, \ldots, a_n, where a_k is the number of the -th athlete found in pre-order traversal.

The third line contains a list b_1, b_2, \ldots, b_n, where b_k is the number of the -th athlete found in post-order traversal.

The fourth line contains a list c_1, c_2, \ldots, c_n, where c_k 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 0 \le s < 999\ 999\ 937 and for which t − s is divisible by .

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Example 3

Loading
Loading