Under construction

Still being built. Things may change

Median

Time limit5sMemory limit1024 MB

Summary
Count the ways to fill the unknown values in a sequence so that the recursive three-way magicThrees procedure returns the true median, modulo 10^9 + 7.
Level

Hard8 of 10

Topics
Divide and conquer, Dynamic programming, Combinatorics
Solved
No attempts yet

Problem

Yaawn... you really shouldn't have played on ByteStation till 3 AM. You begin to slowly drift away, immersed by the monotone voice of your algorithms professor...

Wait. What happened? It seems that you fell asleep during online classes, and slept through the entire afternoon, night, and the next morning. But that means... the exam! Barely catching your breath, you burst into the classroom precisely when the professor is beginning to explain the problem statement.

The task at the exam is to compute the median of a sequence, defined as the middle element in the sorted order (if the length of the sequence is even, the median is the smaller of the two middle values). You are supposed to write down the pseudocode of a solution, and then simulate it on an example sequence provided by the professor.

Reaching into the depths of your memory, you seem to recall something like that appearing during the lecture. Some sort of magic algorithm... magic threes? The memory makes you sleepy again. You somehow split the sequence into parts, solve them recursively, and then combine...?

Based on the bits and pieces you remembered, you came up with the following algorithm:

function magicThrees(sequence)
  if the length of the sequence is no more than 2 then
    return the smallest value in sequence
  otherwise
    part_1, part_2, part_3 = splitIntoThreeParts(sequence)
    median_i = magicThrees(part_i) dla i = 1, 2, 3
    return the median of [median_1, median_2, median_3]

where splitIntoThreeParts divides the sequence into three connected fragments, with lengths as close to each other as possible. Concretely, the fragments have lengths [s,s,s][s, s, s], [s+1,s,s][s + 1, s, s] or [s+1,s+1,s][s + 1, s + 1, s], depending on the length of the original sequence. For example, the sequence [8, 2, 6, 6, 3, 5, 7, 1] is divided into [8, 2, 6], [6, 3, 5] and [7, 1].

After leaving the exam, you realized that your algorithm is not so magical after all, as it does not always work. Maybe, at least, it has worked on the example sequence from the exam... Unfortunately, your memory of that sequence is as fuzzy as your memory of the algorithm. You remember almost all elements of the sequence, but you are not sure about a few of the values. You do remember the overall bounds on all values in the sequence: all elements were supposed to lie in the closed interval [0,m−1][0, m-1].

Calculate the number of ways to choose the values you do not know, such that magicThrees executed on the resulting sequence returns the correct median (as defined above). As the answer may be very large, output it modulo 109+710^9 + 7.

Input

The first line of input contains the number of test cases zz. The descriptions of the test cases follow.

The first line of a test case contains two integers nn, mm (n≥1n \ge 1, 1≤m≤1091 \le m \le 10^9), denoting the length of the test sequence and the bound on the values of its elements.

The second line contains the test sequence, described as nn integers from the range [−1,m−1][-1, m-1], where elements with unknown values are denoted by −1-1.

If we denote the number of unknown values by qq, then every test case belongs to one of the following groups:

  • 1≤z≤1001 \le z \le 100, 1≤q≤101 \le q \le 10, n≤34=81n \le 3^4 = 81
  • z=15z = 15, 1≤q≤201 \le q \le 20, n≤35=243n \le 3^5 = 243
  • z=3z = 3, q=30q = 30, n≤38=6 561n \le 3^8 = 6\,561

Output

For every test case, output a single integer rr (0≤r<109+70 \le r < 10^9 + 7), the answer to the question posed in the problem statement.

Hints

In the first test case, magicThrees returns the correct median irrespective of the two unknown values, so the answer is 102=10010^2 = 100.

In the second test case, magicThrees returns the correct median if and only if the unknown value is not larger than 2020, which gives 2121 ways.

In the third test case, magicThrees returns an incorrect median if both unknown values are smaller than 1010, or both are larger, which gives 1002−(102+892)=1979100^2 - (10^2 + 89^2) = 1979 ways.

Examples1

  1. Example 1

    Input
    3
    3 10
    -1 -1 3
    4 50
    10 20 -1 40
    5 100
    -1 10 10 -1 20
    
    Expected output
    100
    21
    1979