CF BUDDY
← Problems·

1917D · Yet Another Inversions Problem

2300 · combinatorics, data structures, dp

Problem: You are given a permutation p0,p1,,pn1p_0, p_1, \ldots, p_{n-1} of odd integers from 11 to 2n12n-1 and a permutation q0,q1,,qk1q_0, q_1, \ldots, q_{k-1} of integers from 00 to k1k-1.

An array a0,a1,,ank1a_0, a_1, \ldots, a_{nk-1} of length nknk is defined as follows:

aik+j=pi2qja_{i \cdot k+j}=p_i \cdot 2^{q_j} for all 0i<n0 \le i < n and all 0j<k0 \le j < k

For example, if p=[3,5,1]p = [3, 5, 1] and q=[0,1]q = [0, 1], then a=[3,6,5,10,1,2]a = [3, 6, 5, 10, 1, 2].

Note that all arrays in the statement are zero-indexed. Note that each element of the array aa is uniquely determined.

Find the number of inversions in the array aa. Since this number can be very large, you should find only its remainder modulo 998244353998\,244\,353.

An inversion in array aa is a pair (i,j)(i, j) (0i<j<nk0 \le i < j < nk) such that ai>aja_i > a_j.

Input Format: The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn and kk (1n,k21051 \le n, k \le 2 \cdot 10^5) — the lengths of arrays pp and qq.

The second line of each test case contains nn distinct integers p0,p1,,pn1p_0, p_1, \ldots, p_{n-1} (1pi2n11 \le p_i \le 2n-1, pip_i is odd) — the array pp.

The third line of each test case contains kk distinct integers q0,q1,,qk1q_0, q_1, \ldots, q_{k-1} (0qi<k0 \le q_i < k) — the array qq.

It is guaranteed that the sum of nn over all test cases doesn't exceed 21052 \cdot 10^5 and the sum of kk over all test cases doesn't exceed 21052 \cdot 10^5.

Output Format: For each test case, output one integer: the number of inversions in array aa modulo 998244353998\,244\,353.

Note: In the first test case, array aa is equal to [3,6,5,10,1,2][3, 6, 5, 10, 1, 2]. There are 99 inversions in it: (0,4)(0, 4), (0,5)(0, 5), (1,2)(1, 2), (1,4)(1, 4), (1,5)(1, 5), (2,4)(2, 4), (2,5)(2, 5), (3,4)(3, 4), (3,5)(3, 5). Note that these are pairs (i,j)(i, j) such that i<ji < j and ai>aja_i > a_j.

In the second test case, array aa is equal to [8,4,1,2,24,12,3,6,40,20,5,10][8, 4, 1, 2, 24, 12, 3, 6, 40, 20, 5, 10]. There are 2525 inversions in it.

In the third test case, array aa is equal to [1,2,4,8,16][1, 2, 4, 8, 16]. There are no inversions in it.

Sample Cases

Case 1

Input

4
3 2
3 5 1
0 1
3 4
1 3 5
3 2 0 1
1 5
1
0 1 2 3 4
8 3
5 1 7 11 15 3 9 13
2 0 1

Output

9
25
0
104

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback