Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly a_{i} bugs in every line of code that he writes. 

Let's call a sequence of non-negative integers v_1, v_2, ..., v_{n} a plan, if v_1 + v_2 + ... + v_{n} = m. The programmers follow the plan like that: in the beginning the first programmer writes the first v_1 lines of the given task, then the second programmer writes v_2 more lines of the given task, and so on. In the end, the last programmer writes the remaining lines of the code. Let's call a plan good, if all the written lines of the task contain at most b bugs in total.

Your task is to determine how many distinct good plans are there. As the number of plans can be large, print the remainder of this number modulo given positive integer mod.


-----Input-----

The first line contains four integers n, m, b, mod (1 ≤ n, m ≤ 500, 0 ≤ b ≤ 500; 1 ≤ mod ≤ 10^9 + 7) — the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer.

The next line contains n space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 500) — the number of bugs per line for each programmer.


-----Output-----

Print a single integer — the answer to the problem modulo mod.


-----Examples-----
Input
3 3 3 100
1 1 1

Output
10

Input
3 6 5 1000000007
1 2 3

Output
0

Input
3 5 6 11
1 2 1

Output
0

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Dynamic programming


Solutions:
import os
import sys
from io import BytesIO, IOBase

def main():
	(n, m, b, mod) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp = [[0] * (b + 1) for _ in range(m + 1)]
	dp[0] = [1] * (b + 1)
	for item in arr:
		for r in range(1, m + 1):
			for c in range(item, b + 1):
				dp[r][c] = (dp[r][c] + dp[r - 1][c - item]) % mod
	print(dp[m][b])
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


(n, m, b, mod) = map(int, input().split())
a = list(map(int, input().split()))
dp = [[0 for col in range(b + 1)] for row in range(m + 1)]
dp[0][0] = 1
for i in range(n):
	for j in range(1, m + 1):
		for k in range(a[i], b + 1):
			dp[j][k] = (dp[j][k] + dp[j - 1][k - a[i]]) % mod
ans = 0
for i in range(b + 1):
	ans = (ans + dp[m][i]) % mod
print(ans)


(n, m, b, mod) = list(map(int, input().split()))
a = list(map(int, input().split()))
A = [[0 for i in range(m + 1)] for j in range(b + 1)]
A[0][0] = 1
for i in range(n):
	for j in range(a[i], b + 1):
		for k in range(m):
			A[j][k + 1] = (A[j][k + 1] + A[j - a[i]][k]) % mod
ans = 0
for i in range(b + 1):
	ans = (ans + A[i][m]) % mod
print(ans)


(n, m, b, mod) = map(int, input().split())
a = list(map(int, input().split()))
dp = [[0 for i in range(m + 1)] for j in range(b + 1)]
dp[0][0] = 1
for i in range(n):
	for j in range(a[i], b + 1):
		for k in range(m):
			dp[j][k + 1] = (dp[j][k + 1] + dp[j - a[i]][k]) % mod
res = 0
for i in range(b + 1):
	res = (res + dp[i][m]) % mod
print(res)


def main():
	(n, m, b, mod) = list(map(int, input().split()))
	row_zero = [1] + [0] * b
	b += 1
	dp = [[0] * b for _ in range(m)]
	for a in list(map(int, input().split())):
		cur = row_zero
		for nxt in dp:
			for (i, u) in zip(list(range(a, b)), cur):
				nxt[i] = (nxt[i] + u) % mod
			cur = nxt
	print(sum(dp[-1]) % mod)

def __starting_point():
	main()
__starting_point()


a = list(map(int, input().split()))
n = a[0]
m = a[1]
b = a[2]
mod = a[3]
ac = list(map(int, input().split()))
ac = [0] + ac
dp = [[[0 for k in range(b + 1)] for _ in range(m + 1)] for z in range(2)]
for i in range(n + 1):
	for x in range(b + 1):
		dp[i % 2][0][x] = 1
for i in range(1, n + 1):
	for j in range(1, m + 1):
		for x in range(b + 1):
			if ac[i] <= x:
				dp[i % 2][j][x] = (dp[(i - 1) % 2][j][x] + dp[i % 2][j - 1][x - ac[i]]) % mod
			else:
				dp[i % 2][j][x] = dp[(i - 1) % 2][j][x] % mod
print(dp[n % 2][m][b])


import sys
import copy
input = sys.stdin.readline
(n, m, b, mod) = map(int, input().split())
a = list(map(int, input().split()))
dp = [[0] * (m + 1) for i in range(b + 1)]
dp[0][0] = 1
for i in range(n):
	for j in range(a[i], b + 1):
		for k in range(1, m + 1):
			dp[j][k] = (dp[j][k] + dp[j - a[i]][k - 1]) % mod
ans = 0
for i in range(b + 1):
	ans += dp[i][m]
	ans %= mod
print(ans)


def main():
	(n, m, b, mod) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp = [[0 for _ in range(b + 1)] for _ in range(m + 1)]
	dp[0] = [1] * (b + 1)
	for item in arr:
		for x in range(1, m + 1):
			for y in range(item, b + 1):
				dp[x][y] = (dp[x][y] + dp[x - 1][y - item]) % mod
	print(dp[m][b])
main()


import sys
input = sys.stdin.buffer.readline
(n, lines, bugs, mod) = map(int, input().split())
arr = list(map(int, input().split()))
dp = [[0 for i in range(bugs + 1)] for j in range(lines + 1)]
for i in range(n):
	if arr[i] <= bugs:
		dp[1][arr[i]] += 1
	for j in range(lines):
		for k in range(bugs + 1):
			if dp[j][k] > 0 and k + arr[i] <= bugs:
				dp[j + 1][k + arr[i]] = (dp[j + 1][k + arr[i]] + dp[j][k]) % mod
print(sum(dp[lines]) % mod)


(n, m, b, mod) = map(int, input().split())
a = list(map(int, input().split()))
dp = [[0 for i in range(b + 1)] for j in range(m + 1)]
dp[0][0] = 1
for i in range(n):
	for ld in range(m):
		for bugs in range(b + 1):
			if bugs + a[i] <= b:
				dp[ld + 1][bugs + a[i]] = (dp[ld + 1][bugs + a[i]] + dp[ld][bugs]) % mod
ans = 0
for i in range(0, b + 1):
	ans = (ans + dp[m][i]) % mod
print(ans)


import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

def solve(N, A, M, bugs, MOD):
	dp = [[[0 for _ in range(bugs + 1)] for _ in range(M + 1)] for _ in range(2)]
	for j in range(1, M + 1):
		if A[0] * j <= bugs:
			dp[1][j][A[0] * j] = 1
	dp[1][0][0] = 1
	dp[0][0][0] = 1
	for i in range(2, N + 1):
		for j in range(1, M + 1):
			for b in range(bugs + 1):
				s = dp[i % 2][j - 1][b - A[i - 1]] if b >= A[i - 1] else 0
				s += dp[(i + 1) % 2][j][b]
				s %= MOD
				dp[i % 2][j][b] = s
	return sum(dp[N % 2][M]) % MOD
(N, M, B, MOD) = map(int, input().split())
A = [int(x) for x in input().split()]
print(solve(N, A, M, B, MOD))