#P1984C2. Magnitude (Hard Version)
Magnitude (Hard Version)
No submission language available for this problem.
Description
The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.
You are given an array of length . Start with . Then, for each from to (in increasing order) do exactly one of the following:
- Option : set to .
- Option : set to , where is the absolute value of .
Let the maximum final value of after the procedure described above be equal to . Find the number of unique procedures that result in . Two procedures are different if at any index , one procedure chose option and another chose option , even if the value of is equal for both procedures after that turn.
Since the answer may be large, output it modulo .
The first line contains a single integer () — the number of test cases.
The first line of each test case contains a single integer ().
The second line of each test case contains integers ().
The sum of over all test cases does not exceed .
For each test case, output a single integer — the number of unique procedures that result in , modulo .
Input
The first line contains a single integer () — the number of test cases.
The first line of each test case contains a single integer ().
The second line of each test case contains integers ().
The sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the number of unique procedures that result in , modulo .
Note
In the first test case, it can be shown that our maximal final value of is . There are ways to achieve this because in order to get , we have to take absolute value at indices or , or both, resulting in ways. For the other two indices, it doesn't change the value whether we take absolute value or not, so we have ways for them. In total, we have ways.
In the second test case, taking the absolute value will never change anything, so we can either take absolute value or not, for every index. This gives us possible ways.