Under construction

Still being built. Things may change

Counting Haybales

Time limit1sMemory limit1024 MB

Summary
Given stacks of haybales, a move transfers a top haybale between adjacent stacks whose heights differ by one. Count the reachable configurations modulo 1e9+7.
Level

Hard8 of 10

Topics
Dynamic programming, Sorting, Implementation
Solved
No attempts yet

Problem

Bessie the cow is causing trouble in Farmer John's barn again. FJ has NN (1≤N≤50001 \le N \le 5000) stacks of haybales. For each i∈[1,N]i \in [1, N], the ii-th stack has hih_i (1≤hi≤1091 \le h_i \le 10^9) haybales. Bessie does not want any haybales to fall, so the only operation she can perform is the following:

  • If two adjacent stacks differ in height by exactly one, she can move the top haybale of the taller stack to the shorter stack.

After performing this operation any finite number of times, how many configurations can be obtained? Print the count modulo 109+710^9+7. Two configurations are the same if every stack ii has the same number of haybales in both.

Input

The first line contains TT (1≤T≤101 \le T \le 10), the number of independent test cases. All of them must be solved to get one input correct.

Each test case consists of NN, followed by a sequence of NN heights. The sum of NN over all test cases does not exceed 50005000.

Output

Print TT lines, one for each test case.

Hint

For the first test case, the four possible configurations are:

(2,2,2,3),(2,2,3,2),(2,3,2,2),(3,2,2,2).(2,2,2,3), (2,2,3,2), (2,3,2,2), (3,2,2,2).

For the second test case, the four possible configurations are:

(2,3,3,1),(3,2,3,1),(3,3,2,1),(3,3,1,2).(2,3,3,1), (3,2,3,1), (3,3,2,1), (3,3,1,2).

Examples1

  1. Example 1

    Input
    7
    4
    2 2 2 3
    4
    3 3 1 2
    4
    5 3 4 2
    6
    3 3 1 1 2 2
    6
    1 3 3 4 1 2
    6
    4 1 2 3 5 4
    10
    1 5 6 6 6 4 2 3 2 5
    
    Expected output
    4
    4
    5
    15
    9
    8
    19