#P1811G2. Vlad and the Nice Paths (hard version)
Vlad and the Nice Paths (hard version)
Description
This is hard version of the problem, it differs from the easy one only by constraints on and .
Vlad found a row of tiles and the integer . The tiles are indexed from left to right and the -th tile has the color . After a little thought, he decided what to do with it.
You can start from any tile and jump to any number of tiles right, forming the path . Let's call the path of length nice if:
- can be divided into blocks of length exactly , that is, is divisible by ;
- ;
- ;
- ;
Your task is to find the number of nice paths of maximum length. Since this number may be too large, print it modulo .
The first line of each test contains the integer () — the number of test cases in the test.
The first line of each test case contains two integers and () — the number of tiles in a row and the length of the block.
The second line of each test case contains integers () — tile colors.
It is guaranteed that the sum of over all test cases does not exceed .
Print numbers, each of which is the answer to the corresponding test case — the number of nice paths of maximum length modulo .
Input
The first line of each test contains the integer () — the number of test cases in the test.
The first line of each test case contains two integers and () — the number of tiles in a row and the length of the block.
The second line of each test case contains integers () — tile colors.
It is guaranteed that the sum of over all test cases does not exceed .
Output
Print numbers, each of which is the answer to the corresponding test case — the number of nice paths of maximum length modulo .
Note
In the first sample, it is impossible to make a nice path with a length greater than .
In the second sample, we are interested in the following paths:
In the third example, any path of length is nice.