#P7476. [2017年杭电多校]Wavel Sequence
[2017年杭电多校]Wavel Sequence
Wavel Sequence
Problem Description
Have you ever seen the wave? It's a wonderful view of nature. Little Q is attracted to such wonderful thing, he even likes everything that looks like wave. Formally, he defines a sequence as ''wavel'' if and only if Picture from Wikimedia Commons Now given two sequences and , Little Q wants to find two sequences and , where always holds and sequence is ''wavel''. Moreover, Little Q is wondering how many such two sequences and he can find. Please write a program to help him figure out the answer.
Input
The first line of the input contains an integer , denoting the number of test cases. In each test case, there are integers in the first line, denoting the length of and . In the next line, there are integers , denoting the sequence . Then in the next line, there are integers , denoting the sequence .
Output
For each test case, print a single line containing an integer, denoting the answer. Since the answer may be very large, please print the answer modulo .
Sample Input
1
3 5
1 5 3
4 1 1 5 3
Sample Output
10
Hint
(1)f=(1),g=(2). (2)f=(1),g=(3). (3)f=(2),g=(4). (4)f=(3),g=(5). (5)f=(1,2),g=(2,4). (6)f=(1,2),g=(3,4). (7)f=(1,3),g=(2,5). (8)f=(1,3),g=(3,5). (9)f=(1,2,3),g=(2,4,5). (10)f=(1,2,3),g=(3,4,5).
Source
2017 Multi-University Training Contest - Team 4