Born Slippy

아직 제출이 없습니다시간 제한6초메모리 제한256 MB

문제

Professor Zhang has a rooted tree with vertices conveniently labeled by 1,2,,n1, 2, \ldots, n. The ii-th vertex has an integer weight w_iw\_i.

For each s1,2,,ns \in \\{1, 2, \ldots, n\\}, Professor Zhang wants to find a sequence of vertices v_1,v_2,,v_mv\_1, v\_2, \ldots, v\_m such that:

  • v_1=sv\_1 = s and v_iv\_i is the ancestor of v_i1v\_{i - 1} for each 1<im1 < i \le m,
  • the value f(s)=w_v_1+_i=2m(w_v_iopw_v_i1)f(s) = w\_{v\_1} + \sum\limits\_{i = 2}^{m} (w\_{v\_i} \mathrm{op} w\_{v\_{i - 1}}) is maximum possible. Here, operation xopyx \mathrm{op} y is the bitwise AND, OR, or XOR operation on two integers.

입력

There are multiple test cases. The first line of input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn and a string op\mathrm{op} (2n2162 \le n \le 2^{16}, opAND,OR,XOR\mathrm{op} \in \\{\mathtt{AND}, \mathtt{OR}, \mathtt{XOR}\\}): the number of vertices and the operation. The second line contains nn integers w_1,w_2,,w_nw\_1, w\_2, \ldots, w\_n (0w_i<2160 \le w\_i < 2^{16}). The third line contains n1n - 1 integers p_2,p_3,,p_np\_2, p\_3, \ldots, p\_n (1p_i<i1 \le p\_i < i) where p_ip\_i is the parent of vertex ii.

There are about 300300 test cases, and the sum of nn in all the test cases is no more than 10610^6.

출력

For each test case, output the integer S=(_i=1nif(i))S = (\sum\limits\_{i = 1}^{n}{i \cdot f(i)}) modulo 109+710^9 + 7.