J. Similar Subsequence
For given sequence A = (a1, a2, … , an), a sequence S = (s1, s2, … , sn) has shape A if and only if:
• si = min{si
, si+1, … , sn} for all ai = 0;
• si = max{si
, si+1, … , sn} for all ai = 1.
Given sequence B = (b1, b2, … , bm), Bobo would like to know the number of subsequences of length n which
have shape A modulo (109 + 7).
Input
The input contains zero or more test cases and is terminated by end-of-file. For each test case:
The first line contains two integers n and m.
The second line contains n integers a1, a2, … , an.
The thrid line contains m integers b1, b2, … , bm.
• The number of test cases does not exceed 10.
• 1 ≤ n ≤ 20
• 1 ≤ m ≤ 500
• 0 ≤ ai ≤ 1
• 1 ≤ bi ≤ m
• b1, b2, … , bm are distinct.
Output
For each case, output an integer which denotes the number of subsequences modulo (109 + 7).
Sample Input
2 3
0 0
1 2 3
3 5
1 0 1
4 1 3 2 5
Sample Output
3
2
Note
For the first sample, all three subsequences of length 2 are of shape A.