Professor Zhang has a rooted tree with vertices conveniently labeled by 1,2,…,n. The i-th vertex has an integer weight w_i.
For each s∈1,2,…,n, Professor Zhang wants to find a sequence of vertices v_1,v_2,…,v_m such that:
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains an integer n and a string op (2≤n≤216, op∈AND,OR,XOR): the number of vertices and the operation. The second line contains n integers w_1,w_2,…,w_n (0≤w_i<216). The third line contains n−1 integers p_2,p_3,…,p_n (1≤p_i<i) where p_i is the parent of vertex i.
There are about 300 test cases, and the sum of n in all the test cases is no more than 106.
For each test case, output the integer S=(∑_i=1ni⋅f(i)) modulo 109+7.