Digit Longest Increasing Subsequences

All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Recently Chef learned about Longest Increasing Subsequence. To be precise, he means longest strictly increasing subsequence, when he talks of longest increasing subsequence. To check his understanding, he took his favorite ndigit number and for each of its n digits, he computed the length of the longest increasing subsequence of digits ending with that digit. Then he stored these lengths in an array named LIS.
For example, let us say that Chef's favourite 4digit number is 1531, then the LIS array would be [1, 2, 2, 1]. The length of longest increasing subsequence ending at first digit is 1 (the digit 1 itself) and at the second digit is 2 ([1, 5]), at third digit is also 2 ([1, 3]), and at the 4th digit is 1 (the digit 1 itself).
Now Chef is wondering, how many different ndigit numbers (without leading zeros) are there that have exactly the given LIS array? As this number could be very large, output it modulo (10^{9} + 7).
Input
The first line of the input contains an integer T denoting the number of test cases.
For each test case, the first line contains an integer n denoting the number of digits in Chef's favourite number.
The second line will contain n space separated integers denoting LIS array, i.e. LIS_{1}, LIS_{2}, ..., LIS_{n}.
Output
For each test case, output a single integer ― count of different ndigit numbers having the given LIS array, modulo (10^{9} + 7).
Constraints
 1 ≤ T ≤ 2 000
 1 ≤ n ≤ 1 000
 Sum of n over all T testcases is denoted by S
 1 ≤ S ≤ 10 000
 It is guaranteed that at least one ndigit number having the given LIS array exists.
Example
Input: 4 1 1 2 1 2 2 1 1 3 1 2 3 Output: 10 36 54 84
Explanation
Example case 1. All onedigit numbers have the same LIS array, so the answer is 10: 0, 1, 2, 3, ..., 9.
Example cases 2 & 3. Overall we have 90 twodigit numbers (from 10 to 99). Numbers with second digit strictly greater than first digit have LIS array [1, 2] and there are 36 such numbers. All the other twodigit numbers have LIS array [1, 1].
An example of how array LIS is constructed
We will take 7digit number 1730418, its LIS array is [1, 2, 2, 1, 3, 2, 4]:
index  LIS  length 
1  1730418  1 
2  1730418  2 
3  1730418  2 
4  1730418  1 
5  1730418  3 
6  1730418  2 
7  1730418  4 
Author:  alex_2oo8 
Tester:  kingofnumbers 
Editorial  https://discuss.codechef.com/problems/DIGITLIS 
Tags  alex_2oo8, cook78, easymedium, lis 
Date Added:  5082016 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, PYP3, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 