In a tournament, $n$ players play against each other exactly once. Each game results in exactly one player winning. There are no ties. You have been given a scorecard containing the scores of each player at the end of the tournament. The score of a player is the total number of games the player won in the tournament. However, the scores of some players might have been erased from the scorecard. How many possible scorecards are consistent with the input scorecard?

Input Format

The first line contains a single integer $\boldsymbol{\boldsymbol{t}}$ denoting the number of test cases. $\boldsymbol{\boldsymbol{t}}$ test cases follow.  

The first line of each test case contains a single integer $n$. The second line contains $n$ space-separated integers $s_1,s_2,\ldots,s_n$. $s_i$ denotes the score of the $\boldsymbol{i}$th player. If the score of the $\boldsymbol{i}$th player has been erased, it is represented by $-1$.

Constraints

$1\leq t\leq20$
$1\leq n\leq40$
$-1\leq s_i<n$

Output Format

For each test case, output a single line containing the answer for that test case modulo $10^9+7$. 

Sample Input 0
5
3
-1 -1 2
3
-1 -1 -1
4
0 1 2 3
2
1 1
4
-1 -1 -1 2

Sample Output 0
2
7
1
0
12

Explanation 0

For the first case, there are 2 scorecards possible: (0,1,2) or (1,0,2). 

For the second case, the valid scorecards are (1,1,1), (0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0). 

For the third case, the only valid scorecard is (0,1,2,3). 

For the fourth case, there is no valid scorecard. It is not possible for both players to have score of 1. 

For the fifth case, 6-variations of {(3,1,0)[2]}, and 3 variations each of {(2,2,0)[2]} and {(2,1,1)[2]}.

Difficulty: HARD
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
cs = int(input())

def binom(n, r):
	if r < 0 or n < r:
		return 0
	elif n == r or r == 0:
		return 1
	else:
		return n * binom(n - 1, r - 1) // r
for c in range(cs):
	np = int(input())
	sc = [int(i) for i in input().strip().split() if int(i) != -1]
	if sum(sc) > np * (np - 1) // 2 or sc.count(0) > 1:
		print(0)
		continue
	if len(sc) == np:
		print(1)
		continue
	(n, r) = (np - len(sc), np * (np - 1) // 2 - sum(sc))
	rt = binom(r - 1, r - n)
	if 0 not in sc:
		rt += n * binom(r - 1, r - n + 1)
	print(rt)


def totalScore(n):
	return (n - 1) * n // 2

def positiveSum(ar):
	ar = [max(0, i) for i in ar]
	return sum(ar)

def isCorrectTable(table):
	if sum(table) == totalScore(len(table)) and max(table) < len(table):
		return True
	else:
		return False

def f(table, index=0):
	if len(table) == index:
		if isCorrectTable(table):
			return 1
		else:
			return 0
	elif table[index] > 0:
		return f(table, index + 1)
	else:
		posSum = positiveSum(table)
		n = len(table)
		s = 0
		r = min(n, totalScore(n) - posSum) + 1
		for i in range(r):
			t = table[:]
			t[index] = i
			s += f(t, index + 1)
		return s
for t in [1] * int(input()):
	n = int(input())
	table = list(map(int, input().split()))
	print(f(table, 0) % 1000000007)


def modulo(n):
	return n % (10 ** 9 + 7)

def solveThis(remaining_sum, cnt):
	if (remaining_sum, cnt) in dic:
		return dic[remaining_sum, cnt]
	ans = 0
	if remaining_sum == 0:
		return 1
	if remaining_sum < 0:
		return 0
	if cnt == 0:
		return 0
	for i in range(N - 1, -1, -1):
		ans = modulo(modulo(ans) + modulo(solveThis(remaining_sum - i, cnt - 1)))
	dic[remaining_sum, cnt] = ans
	return ans
T = int(input())
for _ in range(T):
	N = int(input())
	nums = list(map(int, input().split()))
	actul_ans = N * (N - 1)
	actul_ans = actul_ans // 2
	count = 0
	ans = 0
	dic = {}
	for i in nums:
		if i == -1:
			count += 1
		else:
			ans += i
	remaining_sum = actul_ans - ans
	if remaining_sum < 0:
		print(0)
		continue
	if remaining_sum == 0:
		print(1)
		continue
	print(solveThis(remaining_sum, count))