#P1442B. Identify the Operations

    ID: 5656 Type: RemoteJudge 2000ms 512MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>combinatoricsdata structuresdsugreedyimplementation*1800

Identify the Operations

No submission language available for this problem.

Description

We start with a permutation a1,a2,,ana_1, a_2, \ldots, a_n and with an empty array bb. We apply the following operation kk times.

On the ii-th iteration, we select an index tit_i (1tini+11 \le t_i \le n-i+1), remove atia_{t_i} from the array, and append one of the numbers ati1a_{t_i-1} or ati+1a_{t_i+1} (if ti1t_i-1 or ti+1t_i+1 are within the array bounds) to the right end of the array bb. Then we move elements ati+1,,ana_{t_i+1}, \ldots, a_n to the left in order to fill in the empty space.

You are given the initial permutation a1,a2,,ana_1, a_2, \ldots, a_n and the resulting array b1,b2,,bkb_1, b_2, \ldots, b_k. All elements of an array bb are distinct. Calculate the number of possible sequences of indices t1,t2,,tkt_1, t_2, \ldots, t_k modulo 998244353998\,244\,353.

Each test contains multiple test cases. The first line contains an integer tt (1t1000001 \le t \le 100\,000), denoting the number of test cases, followed by a description of the test cases.

The first line of each test case contains two integers n,kn, k (1k<n2000001 \le k < n \le 200\,000): sizes of arrays aa and bb.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ain1 \le a_i \le n): elements of aa. All elements of aa are distinct.

The third line of each test case contains kk integers b1,b2,,bkb_1, b_2, \ldots, b_k (1bin1 \le b_i \le n): elements of bb. All elements of bb are distinct.

The sum of all nn among all test cases is guaranteed to not exceed 200000200\,000.

For each test case print one integer: the number of possible sequences modulo 998244353998\,244\,353.

Input

Each test contains multiple test cases. The first line contains an integer tt (1t1000001 \le t \le 100\,000), denoting the number of test cases, followed by a description of the test cases.

The first line of each test case contains two integers n,kn, k (1k<n2000001 \le k < n \le 200\,000): sizes of arrays aa and bb.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ain1 \le a_i \le n): elements of aa. All elements of aa are distinct.

The third line of each test case contains kk integers b1,b2,,bkb_1, b_2, \ldots, b_k (1bin1 \le b_i \le n): elements of bb. All elements of bb are distinct.

The sum of all nn among all test cases is guaranteed to not exceed 200000200\,000.

Output

For each test case print one integer: the number of possible sequences modulo 998244353998\,244\,353.

Samples

Sample Input 1

3
5 3
1 2 3 4 5
3 2 5
4 3
4 3 2 1
4 3 1
7 4
1 4 7 3 6 2 5
3 2 4 5

Sample Output 1

2
0
4

Note

$\require{cancel}$

Let's denote as a1a2aiai+1ana1a2ai1ai+1an1a_1 a_2 \ldots \cancel{a_i} \underline{a_{i+1}} \ldots a_n \rightarrow a_1 a_2 \ldots a_{i-1} a_{i+1} \ldots a_{n-1} an operation over an element with index ii: removal of element aia_i from array aa and appending element ai+1a_{i+1} to array bb.

In the first example test, the following two options can be used to produce the given array bb:

  • 123451235125121 2 \underline{3} \cancel{4} 5 \rightarrow 1 \underline{2} \cancel{3} 5 \rightarrow 1 \cancel{2} \underline{5} \rightarrow 1 2; (t1,t2,t3)=(4,3,2)(t_1, t_2, t_3) = (4, 3, 2);
  • 123451235235151 2 \underline{3} \cancel{4} 5 \rightarrow \cancel{1} \underline{2} 3 5 \rightarrow 2 \cancel{3} \underline{5} \rightarrow 1 5; (t1,t2,t3)=(4,1,2)(t_1, t_2, t_3) = (4, 1, 2).

In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to 44, namely number 33, which means that it couldn't be added to array bb on the second step.

In the third example test, there are four options to achieve the given array bb:

  • 14736251436251432543254351 4 \cancel{7} \underline{3} 6 2 5 \rightarrow 1 4 3 \cancel{6} \underline{2} 5 \rightarrow \cancel{1} \underline{4} 3 2 5 \rightarrow 4 3 \cancel{2} \underline{5} \rightarrow 4 3 5;
  • 14736251436251432514251451 4 \cancel{7} \underline{3} 6 2 5 \rightarrow 1 4 3 \cancel{6} \underline{2} 5 \rightarrow 1 \underline{4} \cancel{3} 2 5 \rightarrow 1 4 \cancel{2} \underline{5} \rightarrow 1 4 5;
  • 14736251473251472547254751 4 7 \underline{3} \cancel{6} 2 5 \rightarrow 1 4 7 \cancel{3} \underline{2} 5 \rightarrow \cancel{1} \underline{4} 7 2 5 \rightarrow 4 7 \cancel{2} \underline{5} \rightarrow 4 7 5;
  • 14736251473251472514251451 4 7 \underline{3} \cancel{6} 2 5 \rightarrow 1 4 7 \cancel{3} \underline{2} 5 \rightarrow 1 \underline{4} \cancel{7} 2 5 \rightarrow 1 4 \cancel{2} \underline{5} \rightarrow 1 4 5;