#codefestival2016qualCc. [codefestival_2016_qualC_c]Two Alpinists
[codefestival_2016_qualC_c]Two Alpinists
Problem Statement
Mountaineers Mr. Takahashi and Mr. Aoki recently trekked across a certain famous mountain range. The mountain range consists of mountains, extending from west to east in a straight line as Mt. , Mt. , ..., Mt. . Mr. Takahashi traversed the range from the west and Mr. Aoki from the east.
The height of Mt. is , but they have forgotten the value of each . Instead, for each (), they recorded the maximum height of the mountains climbed up to the time they reached the peak of Mt. (including Mt. ). Mr. Takahashi's record is and Mr. Aoki's record is .
We know that the height of each mountain is a positive integer. Compute the number of the possible sequences of the mountains' heights, modulo .
Note that the records may be incorrect and thus there may be no possible sequence of the mountains' heights. In such a case, output .
Constraints
- ()
- ()
Input
The input is given from Standard Input in the following format:
Output
Print the number of possible sequences of the mountains' heights, modulo .
Sample Input 1
5
1 3 3 3 3
3 3 2 2 2
Sample Output 1
4
The possible sequences of the mountains' heights are:
for a total of four sequences.
Sample Input 2
5
1 1 1 2 2
3 2 1 1 1
Sample Output 2
0
The records are contradictory, since Mr. Takahashi recorded as the highest peak after climbing all the mountains but Mr. Aoki recorded .
Sample Input 3
10
1 3776 3776 8848 8848 8848 8848 8848 8848 8848
8848 8848 8848 8848 8848 8848 8848 8848 3776 5
Sample Output 3
884111967
Don't forget to compute the number modulo .
Sample Input 4
1
17
17
Sample Output 4
1
Some mountain ranges consist of only one mountain.