Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Chef is a big fan of soccer! He loves soccer so much, that he even invented soccer for his pet dogs! Here are the rules of the game:

There are N dogs numerated from 1 to N stay in a line, so dogs i and i + 1 are adjacent. 
There is a ball which dogs will pass around. Initially, dog s has the ball.
A dog with ball can pass it to another dog. If the current pass-strength of dog is x, then it can pass the ball to either  dog i - x or dog i + x (provided such dog/s exist).

To make it even more exciting, Chef created an array A of M positive integers denoting pass strengths. In i-th pass, current pass-strength of the dog making the pass will be given by A_{i}.
Chef asks dogs to execute these M passes one by one. As stated before, dog s will make the first pass, then some other dog and so on till M passes.

Dogs quickly found out that there can be lot of possible sequences of passes which will end up with a dog having the ball. Now each dog asks your help in finding number of different pass sequences which result in this dog ending up ball. Two pass sequences are considered different if after some number of passes they lead the ball to different dogs. As the answer could be quite large, output it modulo 10^{9} + 7 (1000000007).

------ Input ------ 

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains three space separated integers N, M, s denoting the number of dogs, number of pass strengths and number of dog having a ball at the beginning. 
The second line contains M space-separated integers A_{1}, A_{2}, ..., A_{M} denoting the pass strengths.

------ Output ------ 

For each test case, output a single line containing N space-separated integers, where i-th integer should be equal to number of different valid pass sequences leading the ball to i-th dog modulo 10^{9} + 7. 

------ Constraints ------ 

$1 ≤ T ≤ 10$
$1 ≤ N, M ≤ 10^{3}$
$1 ≤ s ≤ N$ 
$1 ≤ A_{i} ≤ 10^{3}$

------ Subtasks ------ 

$Subtask #1 (30 points) : N, M ≤ 10$ 
$Subtask #2 (70 points) : Original constraints$

----- Sample Input 1 ------ 
3

3 2 2

1 2 

3 3 3

1 1 1

3 1 1

3
----- Sample Output 1 ------ 
1 0 1

0 2 0

0 0 0
----- explanation 1 ------ 
Example case 1.
Possible sequence for dog 1 is 2->3->1.
Possible sequence for dog 3 is 2->1->3.

Example case 2.
Possible sequences for dog 2 are 3->2->1->2 and 3->2->3->2.

Example case 3.
There are no valid sequences for such input.

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Mathematics


Solutions:
t = int(input())
MOD = 10 ** 9 + 7
for _ in range(t):
	(n, m, s) = (int(i) for i in input().split())
	s -= 1
	strength = [int(i) for i in input().split()]
	dp = [[0] * n for _ in range(m + 1)]
	dp[0][s] = 1
	elems = {s}
	for i in range(1, m + 1):
		elems2 = set()
		for elem in elems:
			(a, b) = (elem - strength[i - 1], elem + strength[i - 1])
			if a >= 0:
				dp[i][a] = (dp[i][a] + dp[i - 1][elem]) % MOD
				elems2.add(a)
			if b < n:
				dp[i][b] = (dp[i][b] + dp[i - 1][elem]) % MOD
				elems2.add(b)
		elems = elems2
	print(' '.join(map(str, dp[m])))


mod = 10 ** 9 + 7
for _ in range(int(input())):
	(n, m, s) = [int(i) for i in input().split()]
	A = [int(i) for i in input().split()]
	dp1 = [0] * (n + 1)
	dp2 = [0] * (n + 1)
	zeros = [0] * (n + 1)
	dp1[s] = 1
	for j in range(m):
		for i in range(1, n + 1):
			if dp1[i] == 0:
				continue
			if i - A[j] > 0:
				dp2[i - A[j]] += dp1[i]
			if i + A[j] <= n:
				dp2[i + A[j]] += dp1[i]
		dp1 = list(dp2)
		dp2 = list(zeros)
	for i in range(1, n + 1):
		print(dp1[i] % mod, end=' ')
	print()


def chefsoc2():
	for _ in range(int(input())):
		(n, m, s) = map(int, input().split())
		a = list(map(int, input().split()))
		ma = []
		for i in range(n):
			ma.append(0)
		k = ma[:]
		k[s - 1] = 1
		for i in range(m):
			l = ma[:]
			for j in range(n):
				if k[j]:
					if j + a[i] < n:
						if l[j + a[i]]:
							l[j + a[i]] = (l[j + a[i]] + k[j]) % 1000000007
						else:
							l[j + a[i]] = k[j] % 1000000007
					if j - a[i] >= 0:
						if l[j - a[i]]:
							l[j - a[i]] = (l[j - a[i]] + k[j]) % 1000000007
						else:
							l[j - a[i]] = k[j] % 1000000007
			k = l[:]
		for i in k:
			print(i, end=' ')
		print()
chefsoc2()


import sys
import numpy as np

def build_matrix(A):
	n = len(A)
	ret = np.zeros((n, n), dtype=np.int64)
	for (i, k) in enumerate(A):
		if i - k >= 0:
			ret[i - k, i] = 1
		if i + k < n:
			ret[i + k, i] = 1
	return ret
f = sys.stdin
mod = 10 ** 9 + 7
t = int(f.readline())
for i in range(t):
	(n, m, s) = map(int, f.readline().split())
	A = map(int, f.readline().split())
	x = np.zeros(n, dtype=np.int64)
	x[s - 1] = 1
	for k in A:
		y = np.zeros(n, dtype=np.int64)
		y[:-k] = x[k:]
		y[k:] += x[:-k]
		x = y % mod
	print(*list(x))