A thermal decomposition reaction is breakdown of a compound into one or more compounds when heated.

For example, P \rightarrow a\cdot Q + b\cdot R is a decomposition reaction. Here, 1 unit of P is converted into a unit of Q and b unit of R after completion of the reaction.

You are given N compounds numbered from 1 to N. The initial amount of the i^{th} compound is Q_{i}. 

You are also given M equations. Every equation is of the form: 
C_{0} \rightarrow W_{1}\cdot C_{1} + W_{2}\cdot C_{2} + \ldots + W_{X}\cdot C_{X}.

Here:
C_{0} represents the compound which will decompose into X compounds on heating.
C_{i} (1 ≤ i ≤ X ) represents the compounds into which C_{0} is decomposed.
W_{i} (1 ≤ i ≤ X ) represents the number of units of C_{i} produced due to the decomposition of C_{0}.
C_{i} > C_{i-1} for all (1 ≤ i ≤ X).

Note that a compound can decompose in at most one way only.

Find the final amount of each compound after prolonged heating. Since the number can be huge, print the answer modulo 10^{9}+7.

------ Input Format ------ 

- First line will contain two space separated integers N and M - the number of compounds and the number of equations.
- Second line will contain N space separated integers Q_{1}, Q_{2}, ..., Q_{N} - the initial amounts of each compound.
- The next 2\cdot M lines contain the details of the M equations.  Each equation is represented using 2 lines.
- First line of the i^{th} equation contains two space separated integers C_{i,0}, X_{i} - the decomposing compound and the number of compounds in which it decomposes.
- Second line of the i^{th} equation contains 2 \cdot X_{i} space separated integers W_{i,1}, C_{i,1}, W_{i,2}, C_{i,2},… ,W_{i,X_{i}}, C_{i,X_{i}} - the number of units and the types of compounds into which C_{i,0} decomposes. 

------ Output Format ------ 

Print N lines. The i^{th} line contains a single integer denoting the final amount of the i^{th} compound after prolonged heating modulo 10^{9}+7.

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

$0 ≤ M < N ≤ 10^{5}$
$0 ≤ Q_{i} < (10^{9}+7)$
$1 ≤ C_{i,0} < N$
$1 ≤ X_{i} ≤ (N - C_{i,0})$
$1 ≤ W_{i,j} < (10^{9}+7)$, $\forall$ $(1 ≤ j ≤ X_{i})$ and $(1 ≤ i ≤ M)$.
$C_{i, j} < C_{i,j+1}$, $\forall$ $(0 ≤ j < X_{i})$ and $(1 ≤ i ≤ M)$.
$C_{i, 0} < C_{i + 1, 0}$, $\forall$ $(1 ≤ i < M)$ 
- Sum of $X_{i}$ over all test cases does not exceed $2 \cdot 10^{5}$.

----- Sample Input 1 ------ 
4 2
5 10 12 2
1 2
3 2 4 3
3 1
2 4

----- Sample Output 1 ------ 
0
25
0
66

----- explanation 1 ------ 
Test Cse $1$: We have $4$ compounds initially. For better explanation, we refer the compounds $1, 2, 3,$ and $4$ as $A_{1}, A_{2}, A_{3},$ and $A_{4}$ respectively. Initially, the amount of $A_{1}, A_{2}, A_{3},$ and $A_{4}$ present is $5, 10, 12,$ and $2$ respectively.

The two equations are:
- $A_{1} \rightarrow 3 \cdot A_{2} + 4 \cdot A_{3}$
- $A_{3} \rightarrow 2 \cdot A_{4}$

We know that $1$ unit of $A_{1}$ produces $3$ units of $A_{2}$ and $4$ units of $A_{3}$. Thus, the initial $5$ units of $A_{1}$ decompose to $5\cdot 3 = 15$ units of $A_{2}$ and $5\cdot 4 = 20$ units of $A_{3}$. Note that now there are $0$ units of $A_{1}$ left.  
Considering only this reaction, the updated amounts of chemicals would be $[0, 10 + 15, 12 + 20, 2] = [0, 25, 32, 2]$.

We also know that $1$ unit of $A_{3}$ decomposes to $2$ units of $A_{4}$. The total amount of $A_{3}$ available for decomposition would be $32$ units $(12$ units were available initially and $20$ units were formed due to decomposition$)$. Thus these $32$ units would produce $32\cdot 2 = 64$ units of $A_{4}$.

Thus, after prolonged heating, the amounts of compounds are $[0, 25, 0, 64+2] = [0, 25, 0, 66]$.

Difficulty: MEDIUM_HARD
Tags: Mathematics

Starter code:

Solutions:
temp = list(map(int, input().split()))
M = 1000000007
n = temp[0]
m = temp[1]
arraY = list(map(int, input().split()))
for h in range(m):
	temp1 = list(map(int, input().split()))
	countsdd = temp1[0]
	x = temp1[1]
	undervalo = list(map(int, input().split()))
	temp = arraY[countsdd - 1]
	arraY[countsdd - 1] = 0
	for i in range(0, len(undervalo) - 1, 2):
		arraY[undervalo[i + 1] - 1] = (arraY[undervalo[i + 1] - 1] + temp * undervalo[i]) % M
for k in arraY:
	print(k)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
for j in range(m):
	(b, c) = list(map(int, input().split()))
	d = list(map(int, input().split()))
	dec = a[b - 1]
	a[b - 1] = 0
	k = 0
	while k < len(d):
		val = d[k] * dec % 1000000007
		a[d[k + 1] - 1] = (a[d[k + 1] - 1] + val) % 1000000007
		k += 2
for i in a:
	print(i)


mod = 10 ** 9 + 7
(n, m) = map(int, input().split())
nqli = list(map(int, input().split()))
nqli.insert(0, 0)
for i in range(m):
	(c, x) = map(int, input().split())
	cq = nqli[c]
	if cq == 0:
		continue
	nqli[c] = 0
	cxli = list(map(int, input().split()))
	for j in range(0, 2 * x, 2):
		uni = cxli[j]
		com = cxli[j + 1]
		nqli[com] = (cq * uni + nqli[com]) % mod
del nqli[0]
print(*nqli, sep='\n')


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
for i in range(m):
	(c0, w) = map(int, input().split())
	arr = list(map(int, input().split()))
	for j in range(0, 2 * w, 2):
		l[arr[j + 1] - 1] += arr[j] * l[c0 - 1]
	l[c0 - 1] = 0
for i in l:
	print(i % 1000000007)


(n, m) = map(int, input().split())
q = list(map(int, input().split()))
for i in range(m):
	(x, y) = map(int, input().split())
	lst = list(map(int, input().split()))
	for j in range(0, 2 * y, 2):
		r = q[x - 1] * lst[j] % (10 ** 9 + 7)
		q[lst[j + 1] - 1] += r
		q[lst[j + 1] - 1] %= 10 ** 9 + 7
	q[x - 1] = 0
for i in q:
	print(i)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
p = 10 ** 9 + 7
for i in range(m):
	(x, y) = map(int, input().split())
	b = list(map(int, input().split()))
	for j in range(0, 2 * y, 2):
		f1 = b[j] * a[x - 1]
		f1 %= p
		a[b[j + 1] - 1] += f1
		a[b[j + 1] - 1] %= p
	a[x - 1] = 0
for i in a:
	print(i % p)


(n, m) = map(int, input().split())
amount = list(map(int, input().split()))
Reaction = []
for _ in range(m):
	(c, x) = map(int, input().split())
	Decompose = list(map(int, input().split()))
	Reaction.append(([c, x], Decompose))
	amount_c = amount[c - 1]
	amount[c - 1] = 0
	for i in range(1, len(Decompose), 2):
		amount[Decompose[i] - 1] += amount_c * Decompose[i - 1]
for i in range(len(amount)):
	print(amount[i] % (10 ** 9 + 7))


from sys import stdin, stdout
import math
from collections import Counter
import functools
import time

def tr():
	return range(int(line()))

def sm():
	return map(int, line().split())

def ln():
	return list(sm())

def nl():
	return int(line())

def ssm():
	return map(str, line().split())

def line():
	return stdin.readline().rstrip()

def b(x):
	return bin(x).replace('0b', '')

def o(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + '\n')

def osp(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + ' ')

def ol(x):
	stdout.write(' '.join(map(str, x)) + '\n')

def main():
	modulo = 1000000007
	(n, m) = sm()
	quan = ln()
	for _ in range(m):
		(c, x) = sm()
		equation = ln()
		for i in range(0, 2 * x, 2):
			(q, cm) = (equation[i], equation[i + 1])
			quan[cm - 1] += quan[c - 1] * q
		quan[c - 1] = 0
	for i in range(n):
		print(quan[i] % modulo)
main()


MOD = 10 ** 9 + 7
(n, m) = map(int, input().split())
arr = [0] + [int(i) for i in input().split()]
for _ in range(m):
	(c, sub) = map(int, input().split())
	subs = [int(i) for i in input().split()]
	for i in range(0, len(subs), 2):
		arr[subs[i + 1]] += arr[c] * subs[i]
	arr[c] = 0
for x in arr[1:]:
	print(x % MOD)


(n, m) = list(map(int, input().split()))
ar = list(map(int, input().split()))
for _ in range(m):
	(c, x) = list(map(int, input().split()))
	c -= 1
	comp = list(map(int, input().split()))
	for i in range(2 * x):
		if i % 2 != 0:
			ar[comp[i] - 1] += comp[i - 1] * ar[c]
	ar[c] = 0
for i in ar:
	print(i % (10 ** 9 + 7))


MOD = 1000 * 1000 * 1000 + 7

def AddMod(a, b):
	return (a + b) % MOD

def MultMod(a, b):
	return a * b % MOD
(n, m) = [int(s) for s in input().split(' ')]
q = [int(s) for s in input().split(' ')]
decomposition = list()
for _ in range(m):
	(ci, xi) = [int(s) for s in input().split(' ')]
	wc = [int(s) for s in input().split(' ')]
	for j in range(0, len(wc), 2):
		q[wc[j + 1] - 1] = AddMod(q[wc[j + 1] - 1], MultMod(wc[j], q[ci - 1]))
	q[ci - 1] = 0
for x in q:
	print(x)


from sys import stdin, stdout
prime = 10 ** 9 + 7
(n, m) = map(int, stdin.readline().rstrip().split())
array = list(map(int, stdin.readline().rstrip().split()))
t = m
while t > 0:
	(x, y) = map(int, stdin.readline().rstrip().split())
	arr = list(map(int, stdin.readline().rstrip().split()))
	for i in range(1, 2 * y, 2):
		array[arr[i] - 1] += arr[i - 1] * array[x - 1]
	array[x - 1] = 0
	t -= 1
i = 0
while i < n:
	print(array[i] % prime)
	i += 1


(n, m) = list(map(int, input().split()))
initial_amount = list(map(int, input().split()))
ans = [0] * (n + 1)
for i in range(m):
	add = 0
	(c, x) = list(map(int, input().split()))
	amount = list(map(int, input().split()))
	j = 0
	val = initial_amount[c - 1]
	while j < len(amount):
		initial_amount[amount[j + 1] - 1] += val * amount[j]
		j += 2
	initial_amount[c - 1] = 0
mod = pow(10, 9) + 7
for i in initial_amount:
	print(i % mod)


ans = 0
(n_com, n_eq) = list(map(int, input().split()))
quant = list(map(int, input().split()))
eq = []
products = []
for i in range(n_eq):
	(dec, res) = list(map(int, input().split()))
	eq.append([dec, res])
	temp = list(map(int, input().split()))
	products.append(temp)
for i in range(len(eq)):
	parent = eq[i][0] - 1
	n = eq[i][1]
	j = 0
	if quant[parent] > 0:
		while j < 2 * n:
			w = products[i][j]
			indx = products[i][j + 1] - 1
			quant[indx] += quant[parent] * w
			j = j + 2
		quant[parent] = 0
for i in range(n_com):
	ans = quant[i]
	print(ans % (pow(10, 9) + 7))


import copy
import math
(n, m) = map(int, input().split())
initial_amt_n = list(map(int, input().split()))
equations = dict()
for i in range(m):
	(C0, x) = map(int, input().split())
	splitedin = list(map(int, input().split()))
	diction = dict()
	u = 0
	while u < 2 * x - 1:
		diction[splitedin[u + 1] - 1] = splitedin[u]
		u += 2
	equations[C0 - 1] = diction
jj = list(equations.keys())
jj.sort()
for e in jj:
	comp = e
	dic = copy.deepcopy(equations[e])
	if initial_amt_n[comp] != 0:
		for a in dic:
			initial_amt_n[a] += dic[a] * initial_amt_n[comp]
			initial_amt_n[a] %= 1000000007
		initial_amt_n[comp] = 0
for u in initial_amt_n:
	print(int(u % (math.pow(10, 9) + 7)))


ls = list(map(int, input().split()))
ini = list(map(int, input().split()))
for _ in range(ls[1]):
	eqd = list(map(int, input().split()))
	eq = list(map(int, input().split()))
	for i in range(eqd[1]):
		ini[eq[i * 2 + 1] - 1] += eq[i * 2] * ini[eqd[0] - 1]
	ini[eqd[0] - 1] = 0
for x in ini:
	print(x % (10 ** 9 + 7))


try:
	(n, m) = map(int, input().split())
	li = list(map(int, input().split()))
	for _ in range(m):
		(x, y) = map(int, input().split())
		li1 = list(map(int, input().split()))
		temp = li[x - 1]
		li[x - 1] = 0
		for i in range(0, 2 * y, 2):
			li[li1[i + 1] - 1] += li1[i] * temp
	for i in li:
		print(i % 1000000007)
except:
	pass


(n, m) = map(int, input().split())
mm = 1000000007
q = list(map(int, input().split()))
for i in range(m):
	(a, b) = map(int, input().split())
	w = list(map(int, input().split()))
	for j in range(b):
		q[w[2 * j + 1] - 1] = (q[w[2 * j + 1] - 1] % mm + w[2 * j] % mm * (q[a - 1] % mm) % mm) % mm
	q[a - 1] = 0
for i in range(n):
	print(q[i])


data = list(map(int, input().strip().split()))
N = data[0]
M = data[1]
Q = list(map(int, input().strip().split()))
for i in range(M):
	data = list(map(int, input().strip().split()))
	number = data[0]
	X = data[1]
	equation = list(map(int, input().strip().split()))
	for j in range(X):
		Q[equation[2 * j + 1] - 1] += Q[number - 1] * equation[2 * j]
	Q[number - 1] = 0
for x in Q:
	print(x % (10 ** 9 + 7))


(n, m) = map(int, input().split())
amount = {}
l = input().split()
for i in range(n):
	amount[i + 1] = int(l[i])
decomposeEquation = {}
for _ in range(m):
	(a, b) = map(int, input().split())
	decompse = []
	l = [int(x) for x in input().split()]
	for i in range(0, 2 * b, 2):
		decompse.append([l[i], l[i + 1]])
	decomposeEquation[a] = decompse
check = 1
while check:
	check = 0
	for compound in amount:
		if amount[compound] != 0 and compound in decomposeEquation:
			check = 1
			Equation = decomposeEquation[compound]
			for i in Equation:
				amount[i[1]] += i[0] * amount[compound]
			amount[compound] = 0
for i in range(n):
	print(amount[i + 1] % 1000000007)


import sys
(n, m) = map(int, sys.stdin.readline().split())
q = [int(i) for i in sys.stdin.readline().split()]
for i in range(m):
	(ci, xi) = map(int, sys.stdin.readline().split())
	c = [int(i) for i in sys.stdin.readline().split()]
	for j in range(0, len(c) - 1, 2):
		q[c[j + 1] - 1] = q[c[j + 1] - 1] + c[j] * q[ci - 1]
	q[ci - 1] = 0
for k in range(0, n):
	print(q[k] % (10 ** 9 + 7))


(n, m) = list(map(int, input().split()))
amount = list(map(int, input().split()))
for _ in range(m):
	(decomposition_compound, n) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	(i, j) = (0, 1)
	while j < len(l):
		quantity = l[i]
		product = l[j]
		amount[l[j] - 1] = amount[l[j] - 1] + amount[decomposition_compound - 1] * l[i]
		i += 2
		j += 2
	amount[decomposition_compound - 1] = 0
for i in amount:
	print(i % (10 ** 9 + 7))


(n, m) = map(int, input().split())
q = list(map(int, input().split()))
for i in range(m):
	(c, x) = map(int, input().split())
	wc = list(map(int, input().split()))
	l = 1
	for j in range(x):
		q[wc[l] - 1] += wc[l - 1] * q[c - 1]
		l += 2
	q[c - 1] = 0
for i in q:
	print(i % 1000000007)


mod = 10 ** 9 + 7
(n, m) = map(int, input().split())
q = list(map(int, input().split()))
for _ in range(m):
	(ci0, xi) = map(int, input().split())
	wc = list(map(int, input().split()))
	units_we_have = q[ci0 - 1]
	for i in range(0, 2 * xi, 2):
		(wi, ci) = (wc[i], wc[i + 1])
		units_ci_generated = wi * units_we_have
		q[ci - 1] += units_ci_generated
		q[ci0 - 1] = 0
for item in q:
	print(item % mod)


from collections import defaultdict
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
d = defaultdict(lambda : 0)
for i in range(n):
	d[i + 1] = l[i]
mod = 10 ** 9 + 7
for _ in range(m):
	(x, y) = map(int, input().split())
	l = list(map(int, input().split()))
	for i in range(0, 2 * y, 2):
		d[l[i + 1]] = (d[l[i + 1]] + l[i] * d[x]) % mod
	d[x] = 0
ans = 0
for i in range(n):
	print(d[i + 1] % mod)


(n, m) = map(int, input().split())
initial_weights = [int(_) for _ in input().split()]
for i in range(m):
	(c, x) = map(int, input().split())
	coeff = [int(_) for _ in input().split()]
	for j in range(0, 2 * x, 2):
		initial_weights[coeff[j + 1] - 1] += coeff[j] * initial_weights[c - 1]
	initial_weights[c - 1] = 0
for i in initial_weights:
	print(i % (pow(10, 9) + 7))


(compounds, reactions) = map(int, input().split())
quantities = [int(i) for i in input().split()]
for i in range(reactions):
	(compound, products) = map(int, input().split())
	reaction_info = [int(i) for i in input().split()]
	quantity_of_reactant = quantities[compound - 1]
	quantities[compound - 1] = 0
	for j in range(0, len(reaction_info), 2):
		(product, quantity) = (reaction_info[j + 1], reaction_info[j])
		quantities[product - 1] += quantity * quantity_of_reactant
for qu in quantities:
	print(qu % 1000000007)


def mi():
	return map(int, input().split())

def li():
	return list(mi())

def ni():
	return int(input())

def si():
	return str(input())
(n, m) = mi()
q = li()
for i in range(m):
	(a, b) = mi()
	re = li()
	temp = int(q[a - 1])
	q[a - 1] = 0
	for j in range(0, len(re), 2):
		(x, y) = (re[j], re[j + 1])
		q[y - 1] += temp * x
		q[y - 1] %= 1000000007
for i in q:
	print(i)


from sys import stdin
input = stdin.readline

def main():
	(n, m) = map(int, input().split())
	init = list(map(int, input().split()))
	c_ind = []
	p = 0
	c_l = 0
	for _ in range(m):
		(target, x) = map(int, input().split())
		w_c = list(map(int, input().split()))
		for i in range(0, 2 * x, 2):
			init[w_c[i + 1] - 1] += init[target - 1] * w_c[i]
		c_ind.append(target - 1)
		c_l += 1
	for i in range(n):
		if p < c_l and i == c_ind[p]:
			print(0)
			p += 1
		else:
			print(init[i] % 1000000007)
main()


(N, M) = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]
for m in range(M):
	(C, X) = [int(x) for x in input().split()]
	l = [int(x) for x in input().split()]
	W_C = []
	k = 0
	for j in range(X):
		W_C.append([l[k], l[k + 1]])
		k += 2
	for i in range(X):
		Q[int(W_C[i][1]) - 1] += int(W_C[i][0]) * Q[C - 1]
	Q[C - 1] = 0
for i in range(N):
	print(Q[i] % (7 + 10 ** 9))


(n, m) = map(int, input().split())
listt = list(map(int, input().split()))
for _ in range(m):
	(a, b) = map(int, input().split())
	g = listt[a - 1]
	listt[a - 1] = 0
	k = list(map(int, input().split()))
	for i in range(0, 2 * b, 2):
		listt[k[i + 1] - 1] += k[i] * g
for i in listt:
	print(i % (10 ** 9 + 7))


def solution(Q, probs, indices):
	Q = [0] + Q
	for i in indices:
		if Q[i] != 0:
			for j in probs[i]:
				Q[j] += probs[i][j] * Q[i]
			Q[i] = 0
	return Q[1:]
nm = list(map(int, input().split()))
M = nm[1]
N = nm[0]
Q = list(map(int, input().split()))
probs = [{} for __ in range(N + 1)]
indices = []
ind = {i: 0 for i in range(len(Q) + 1)}
final = [0] + Q
for i in range(M):
	inp = list(map(int, input().split()))
	line = inp[0]
	final[line] = 0
	dec = inp[1]
	coeff = list(map(int, input().split()))
	indices.append(line)
	ind[line] = 1
	for j in range(dec):
		probs[line][coeff[j * 2 + 1]] = coeff[j * 2]
sol = solution(Q, probs, indices)
for i in sol:
	print(i % (10 ** 9 + 7))


from collections import defaultdict as dd

def update(qnt, l, a):
	for y in range(0, len(l), 2):
		qnt[l[y + 1] - 1] = qnt[l[y + 1] - 1] + qnt[a] * l[y]
	qnt[a] = 0
(n, m) = list(map(int, input().split()))
qnt = list(map(int, input().split()))
dt = dd(int)
for x in range(m):
	(element, prd) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	dt[element] = l
	for y in range(0, 2 * prd, 2):
		qnt[l[y + 1] - 1] = qnt[l[y + 1] - 1] + qnt[element - 1] * l[y]
		if dt[l[y + 1]] != 0:
			update(qnt, dt[l[y + 1]], l[y + 1])
	qnt[element - 1] = 0
for x in qnt:
	print(x % 1000000007)


(n, e) = map(int, input().split())
l = list(map(int, input().split()))
for j in range(e):
	(c, x) = map(int, input().split())
	k = list(map(int, input().split()))
	i = 0
	while i != x * 2:
		s = 0
		s += l[c - 1] * k[i]
		l[k[i + 1] - 1] += s
		i += 2
	l[c - 1] = 0
for i in l:
	print(i % 1000000007)


(n, m) = map(int, input().split(' '))
q = list(map(int, input().split(' ')))
for i in range(m):
	(c, x) = map(int, input().split(' '))
	wc = list(map(int, input().split(' ')))
	i = 0
	while i < len(wc) - 1:
		q[wc[i + 1] - 1] += wc[i] * q[c - 1]
		i += 2
	q[c - 1] = 0
for j in q:
	print(j % (10 ** 9 + 7))


mod = 1000000007
(n, m) = map(int, input().split())
q = list(map(int, input().strip().split()))
d = {i + 1: q[i] for i in range(n)}
for _ in range(m):
	(c0, x) = map(int, input().split())
	a = list(map(int, input().strip().split()))
	for i in range(1, 2 * x, 2):
		d[a[i]] += d[c0] * a[i - 1]
	d[c0] = 0
for key in d:
	print(d[key] % mod)


def getlist():
	return list(map(int, input().split(' ')))
(n, m) = getlist()
arr = getlist()
const = 1000000007
for i in range(m):
	(c, x) = getlist()
	eq = getlist()
	temp = arr[c - 1]
	arr[c - 1] = 0
	for i in range(0, 2 * x - 1, 2):
		arr[eq[i + 1] - 1] = (arr[eq[i + 1] - 1] + temp * eq[i]) % const
print('\n'.join([str(i) for i in arr]))


from collections import deque
import sys
input = sys.stdin.readline
(num_compounds, num_equations) = [int(x) for x in input().split()]
compound_amounts = [int(x) for x in input().split()]
compound_amounts = {i + 1: x for (i, x) in enumerate(compound_amounts)}
stuff_compound_decomposed_into = dict()
num_units_out = dict()
for i in range(num_equations):
	(compound, num_components_it_decomposes_into) = [int(x) for x in input().split()]
	stuff_compound_decomposed_into[compound] = set()
	arr = [int(x) for x in input().split()]
	for j in range(num_components_it_decomposes_into):
		amount = arr[2 * j]
		to_compound = arr[2 * j + 1]
		stuff_compound_decomposed_into[compound].add(to_compound)
		num_units_out[compound, to_compound] = amount
frontier = deque([compound for compound in compound_amounts if compound_amounts[compound] > 0])
frontier_set = set(frontier)
mod = 10 ** 9 + 7
while frontier:
	compound = frontier.popleft()
	frontier_set.remove(compound)
	if compound not in stuff_compound_decomposed_into:
		continue
	for to_compound in stuff_compound_decomposed_into[compound]:
		compound_amounts[to_compound] = (compound_amounts[to_compound] + compound_amounts[compound] * num_units_out[compound, to_compound]) % mod
		if to_compound not in frontier_set:
			frontier.append(to_compound)
			frontier_set.add(to_compound)
	compound_amounts[compound] = 0
ans = [compound_amounts[compound] for compound in range(1, num_compounds + 1)]
for x in ans:
	print(x)


MOD = 10 ** 9 + 7
(N, M) = map(int, input().split())
Q = [0] + list(map(int, input().split()))
eq = {}
for mx in range(M):
	(s, t) = map(int, input().split())
	ti = list(map(int, input().split()))
	cf = {}
	while ti:
		m = ti.pop()
		cf[ti.pop()] = m
	eq[s] = cf
decomp = True
while decomp:
	decomp = False
	for (r, e) in eq.items():
		q = Q[r]
		if q:
			Q[r] = 0
			decomp = True
			for (c, t) in e.items():
				Q[t] = (Q[t] + q * c) % MOD
print(*Q[1:], sep='\n')


a = list(map(int, input().split()))
n = a[0]
m = a[1]
N = []
N = list(map(int, input().split()))
mod = 1000000007
while m != 0:
	b = list(map(int, input().split()))
	x = b[0]
	y = b[1]
	M = []
	M = list(map(int, input().split()))
	for i in range(0, 2 * y, 2):
		N[M[i + 1] - 1] = (N[M[i + 1] - 1] + M[i] * N[x - 1]) % mod
	N[x - 1] = 0
	m = m - 1
for i in range(n):
	print(N[i])


mod = 10 ** 9 + 7
(n, m) = [int(x) for x in input().split()]
l = [int(i) for i in input().split()]
for i in range(m):
	(c0, x) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	for i in range(1, x + 1):
		l[a[2 * i - 1] - 1] += l[c0 - 1] * a[2 * i - 2] % mod
	l[c0 - 1] = 0
for i in l:
	print(i % mod)


MOD = 10 ** 9 + 7

def kahns_topsort(adj):
	from collections import deque
	in_degrees = [0] * len(adj)
	q = deque()
	result = [-1] * len(adj)
	for neighbors in adj:
		for vertex in neighbors:
			in_degrees[vertex] += 1
	for (vertex, count) in enumerate(in_degrees):
		if count == 0:
			q.append(vertex)
	index = 0
	while q:
		vertex = q.popleft()
		result[index] = vertex
		index += 1
		for neighbor in adj[vertex]:
			in_degrees[neighbor] -= 1
			if in_degrees[neighbor] == 0:
				q.append(neighbor)
	return result if len(result) == len(adj) else []

def solve(initial, adj, wt):
	order = kahns_topsort(adj)
	for cp in order:
		if cp not in wt:
			continue
		for (i, dc) in enumerate(adj[cp]):
			initial[dc] += initial[cp] * wt[cp][i]
		initial[cp] = 0
	return initial

def main():
	for _ in range(1):
		(n, m) = map(int, input().split())
		q = {i: int(val) for (i, val) in enumerate(input().split(), 1)}
		adj = [[] for _ in range(n + 1)]
		wt = {}
		for _ in range(m):
			(dc, x) = map(int, input().split())
			wt[dc] = []
			for (i, char) in enumerate(input().split()):
				val = int(char)
				if i % 2 == 0:
					wt[dc].append(val)
				else:
					adj[dc].append(val)
		result = solve(q, adj, wt)
		for val in result.values():
			print(val % MOD)
main()


(n, m) = list(map(int, input().split()))
reagents = list(map(int, input().split()))
b = reagents.copy()
for i in range(m):
	(reactant, no_of_products) = list(map(int, input().split()))
	data = list(map(int, input().split()))
	products = list()
	coeff = list()
	ind = 0
	while True:
		if ind + 1 > no_of_products * 2 - 1:
			break
		coeff.append(data[ind])
		products.append(data[ind + 1])
		ind += 2
	for j in range(no_of_products):
		b[products[j] - 1] += coeff[j] * b[reactant - 1]
	b[reactant - 1] = 0
for i in b:
	print(i % (10 ** 9 + 7))


(noOfComp, noOfEqns) = map(int, input().split())
quantity = list(map(int, input().split()))
eqnDict = {}
for _ in range(0, noOfEqns):
	(reactant, noOfProducts) = map(int, input().split())
	productArr = list(map(int, input().split()))
	for i in range(0, len(productArr), 2):
		if reactant in eqnDict.keys():
			eqnDict[reactant][productArr[i + 1]] = productArr[i]
		else:
			eqnDict[reactant] = {productArr[i + 1]: productArr[i]}
for reactant in eqnDict.keys():
	for product in eqnDict[reactant].keys():
		coeff = eqnDict[reactant][product]
		quantity[product - 1] += quantity[reactant - 1] * coeff
	quantity[reactant - 1] = 0
quantity = [x % 1000000007 for x in quantity]
[print(x) for x in quantity]


(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
for i in range(0, m):
	(a, b) = map(int, input().split())
	e1 = list(map(int, input().split()))
	t = l1[a - 1]
	l1[a - 1] = 0
	k = 0
	while k < 2 * b - 1:
		l1[e1[k + 1] - 1] += e1[k] * t
		k = k + 2
for i in l1:
	print(i % (pow(10, 9) + 7))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
sys.setrecursionlimit(100000000)
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
mod = 10 ** 9 + 7

def func():
	(n, m) = mul()
	coeff = seq()
	for i in range(m):
		(lhs, rhscnt) = mul()
		cmp = seq()
		for j in range(0, rhscnt * 2, 2):
			coeff[cmp[j + 1] - 1] += coeff[lhs - 1] * cmp[j]
		coeff[lhs - 1] = 0
	for i in range(n):
		print(coeff[i] % mod)
t = 1
for i in range(t):
	func()


(com, eq) = map(int, input().split())
compound = list(map(int, input().split()))
for i in range(eq):
	l = []
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	j = 0
	while j < 2 * k:
		compound[l[j + 1] - 1] += compound[n - 1] * l[j]
		j += 2
	compound[n - 1] = 0
for m in compound:
	print(m % (10 ** 9 + 7))


nm = list(map(int, input().strip().split()))
(n, m) = (nm[0], nm[1])
li = list(map(int, input().strip().split()))
li.insert(0, 0)
mod = 10 ** 9 + 7
for i in range(m):
	l1 = list(map(int, input().strip().split()))
	l2 = list(map(int, input().strip().split()))
	mf = li[l1[0]]
	li[l1[0]] = 0
	j = 0
	while j < len(l2) - 1:
		mf2 = l2[j]
		li[l2[j + 1]] += mf * mf2
		j += 2
for i in li[1:]:
	print(i % mod)


(n, x) = map(int, input().split(' '))
li = list(map(int, input().split(' ')))
for _ in range(x):
	(a, b) = map(int, input().split(' '))
	lis = list(map(int, input().split(' ')))
	i = 1
	while i < 2 * b:
		li[lis[i] - 1] += lis[i - 1] * li[a - 1]
		i += 2
	li[a - 1] = 0
for i in li:
	print(i % (10 ** 9 + 7))


debug = False

def dprint(*args, **kwargs):
	if not debug:
		return
	print('debug:\t' + ' '.join(map(str, args)), **kwargs)
(n, m) = map(int, input().split())
qs = list(map(int, input().split()))
rules = {}
for i in range(m):
	(ci, xi) = map(int, input().split())
	wcs = list(map(int, input().split()))
	wcs = [wcs[i:i + 2] for i in range(0, len(wcs), 2)]
	rules[ci] = wcs
dprint(rules)
dprint('')
hasNewCompound = True
while hasNewCompound:
	hasNewCompound = False
	for r in rules:
		qAmount = qs[r - 1]
		if qAmount > 0:
			hasNewCompound = True
			wcs = rules[r]
			dprint(wcs)
			for (w, c) in wcs:
				qs[c - 1] += qAmount * w
			qs[r - 1] = 0
			dprint(qs)
for q in qs:
	print(q % (10 ** 9 + 7))


(no_of_compounds, no_of_equations) = map(int, input().split())
total_compds = list(range(1, no_of_compounds + 1))
initial_amt = list(map(int, input().split()))
amt = {}
for i in range(no_of_compounds):
	amt[total_compds[i]] = initial_amt[i]
equation_reactants = []
total_no_of_products = []
product_details = []
equation = []
equations = []
for i in range(no_of_equations):
	(reactant, no_of_products) = map(int, input().split())
	equation_reactants.append(reactant)
	total_no_of_products.append(no_of_products)
	products = list(map(int, input().split()))
	product_details.append(products)
for i in range(len(total_no_of_products)):
	k = 0
	equation.append(equation_reactants[i])
	for j in range(total_no_of_products[i]):
		equation.append((product_details[i][k], product_details[i][k + 1]))
		k += 2
	equations.append(equation)
	equation = []
for i in equations:
	for j in range(1, len(i)):
		amt[i[j][1]] += i[j][0] * amt[i[0]]
	amt[i[0]] = 0
for k in amt.values():
	print(k % 1000000007)


(n, m) = map(int, input().split())
compoundlst = list(map(int, input().split()))
for i in range(m):
	(initial, total) = map(int, input().split())
	lst = list(map(int, input().split()))
	temp = compoundlst[initial - 1]
	compoundlst[initial - 1] = 0
	for j in range(1, len(lst), 2):
		compoundlst[lst[j] - 1] += temp * lst[j - 1]
for i in compoundlst:
	print(i % 1000000007)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
for t in range(m):
	(c, x) = map(int, input().split())
	ars = list(map(int, input().split()))
	index = []
	size = []
	for i in range(2 * x):
		if i % 2 == 0:
			size.append(ars[i])
		else:
			index.append(ars[i])
	for j in range(x):
		arr[index[j] - 1] = (arr[index[j] - 1] + size[j] * arr[c - 1]) % 1000000007
	arr[c - 1] = 0
for s in range(n):
	print(arr[s])


(m, n) = map(int, input().split())
l = list(map(int, input().split()))
while n > 0:
	(a, b) = map(int, input().split())
	s = list(map(int, input().split()))
	x = l[a - 1]
	l[a - 1] = 0
	for i in range(1, 2 * b, 2):
		l[s[i] - 1] = l[s[i] - 1] + x * s[i - 1]
	n -= 1
for i in l:
	print(i % (10 ** 9 + 7))


mod = 10 ** 9 + 7
(nc, ne) = map(int, input().split(' '))
amounts = [0] + list(map(int, input().split(' ')))
memo = dict()
degree = [-1] + [0] * (len(amounts) - 1)
for i in range(ne):
	(c0, x) = map(int, input().split(' '))
	val = list(map(int, input().split(' ')))
	units = [v for (i, v) in enumerate(val) if i % 2 == 0]
	ctype = [v for (i, v) in enumerate(val) if i % 2 == 1]
	memo[c0] = (units, ctype)
	for j in ctype:
		degree[j] += 1
while True:
	degree_zero = [i for i in range(len(degree)) if degree[i] == 0 and i in memo]
	if len(degree_zero) == 0:
		break
	for c0 in degree_zero:
		(units, ctype) = memo[c0]
		for j in range(len(ctype)):
			amounts[ctype[j]] = (amounts[ctype[j]] + units[j] * amounts[c0] % mod) % mod
			degree[ctype[j]] -= 1
		amounts[c0] = 0
		degree[c0] = -1
for i in range(len(amounts)):
	amounts[i] = amounts[i] % mod
print('\n'.join(map(str, amounts[1:])))


(n, m) = list(map(int, input().split()))
cmps = list(map(int, input().split()))
for _ in range(m):
	(a, b) = list(map(int, input().split()))
	t = list(map(int, input().split()))
	i = 0
	while i < len(t) - 1:
		cmps[t[i + 1] - 1] += t[i] * cmps[a - 1]
		i += 2
	cmps[a - 1] = 0
for i in cmps:
	print(i % (10 ** 9 + 7))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
(n, m) = lis()
amount = lis()
for _ in range(0, m):
	(c, x) = lis()
	L = lis()
	X = L[1::2]
	W = L[0::2]
	for i in range(x):
		amount[X[i] - 1] = W[i] * amount[c - 1] + amount[X[i] - 1]
	amount[c - 1] = 0
for x in amount:
	print(x % (pow(10, 9) + 7))


Test_case = 1
for test in range(Test_case):
	(n, m) = map(int, input().split())
	query = list(map(int, input().split()))
	temp = []
	for x in range(2 * m):
		temp.append(list(map(int, input().split())))
	for y in range(0, 2 * m, 2):
		(c, a) = (temp[y][0], temp[y][1])
		tt = query[c - 1]
		query[c - 1] = 0
		for z in range(0, 2 * a, 2):
			query[temp[y + 1][z + 1] - 1] += tt * temp[y + 1][z]
	for nn in query:
		print(nn % 1000000007)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
for i in range(m):
	(c, x) = map(int, input().split())
	c -= 1
	lst = list(map(int, input().split()))
	for j in range(0, 2 * x, 2):
		arr[lst[j + 1] - 1] += lst[j] * arr[c]
	arr[c] = 0
for i in range(n):
	print(arr[i] % (10 ** 9 + 7))


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
for i in range(0, m):
	(x, y) = [int(x) for x in input().split()]
	z = [int(x) for x in input().split()]
	b = a[x - 1]
	a[x - 1] = 0
	(j, i) = (1, 0)
	while j <= y:
		s = b * z[i]
		a[z[i + 1] - 1] += s
		i += 2
		j += 1
e = 10 ** 9 + 7
for i in a:
	print(i % e)


(n, m) = map(int, input().split())
Q = list(map(int, input().split()))
mod = pow(10, 9) + 7
for i in range(m):
	(c, x) = map(int, input().split())
	ar = list(map(int, input().split()))
	for j in range(0, 2 * x, 2):
		k = Q[c - 1]
		Q[ar[j + 1] - 1] += k * ar[j]
	Q[c - 1] = 0
for i in range(n):
	print(Q[i] % mod)


import sys
M = 10 ** 9 + 7
i = (map(int, s.split()) for s in sys.stdin)
next(i)
a = [None, *next(i)]
for ((c0, x), j) in zip(i, i):
	q = a[c0]
	a[c0] = 0
	for (w, c) in zip(j, j):
		a[c] = (a[c] + q * w) % M
a = iter(a)
next(a)
print(*a, sep='\n')


def process(quantities, equations):
	dirty = True
	while dirty:
		dirty = False
		for equation in equations:
			compound = equation[0]
			num_products = equation[1]
			weights_products = equation[2]
			converted = quantities[compound]
			if converted == 0:
				continue
			else:
				dirty = True
			quantities[compound] = 0
			for j in range(0, len(weights_products), 2):
				units = weights_products[j]
				product = weights_products[j + 1]
				quantities[product] += converted * units
	for c in range(1, len(quantities)):
		print(quantities[c] % 1000000007)

def get_list(func):
	return list(map(func, input().split()))

def solve():
	(compounds, num_equations) = get_list(int)
	quantities = get_list(int)
	quantities.insert(0, 0)
	equations = []
	for i in range(num_equations):
		(compound, num_products) = get_list(int)
		weights_products = get_list(int)
		equations.append((compound, num_products, weights_products))
	process(quantities, equations)
solve()


(n, m) = [int(i) for i in input().split()]
compounds = [0] + [int(i) for i in input().split()]
for _ in range(m):
	(c0, x) = [int(i) for i in input().split()]
	eqn = input().split()
	n = compounds[c0]
	compounds[c0] = 0
	for (w, c) in [(int(eqn[i]), int(eqn[i + 1])) for i in range(0, 2 * x, 2)]:
		compounds[c] += n * w
for i in compounds[1:]:
	print(i % (10 ** 9 + 7))


def sol(M, Q):
	m = 1000000007
	for i in range(M):
		(a, b) = map(int, input().split())
		c = list(map(int, input().split()))
		x = 0
		for j in range(0, b):
			Q[c[x + 1] - 1] = Q[c[x + 1] - 1] + c[x] * Q[a - 1]
			x = x + 2
		Q[a - 1] = 0
	for k in Q:
		print(k % m)
(N, M) = map(int, input().split())
Q = list(map(int, input().split()))
sol(M, Q)


mod = 10 ** 9 + 7
(n, m) = list(map(int, input().strip().split()))
ans = [0] * n
c = list(map(int, input().strip().split()))
for i in range(m):
	(ai, xi) = list(map(int, input().strip().split()))
	rhs = list(map(int, input().strip().split()))
	j = 0
	while j < 2 * xi:
		sum = 0
		w = rhs[j]
		aj = rhs[j + 1]
		sum = w * c[ai - 1] % mod
		c[aj - 1] = (c[aj - 1] + sum) % mod
		j += 2
	c[ai - 1] = ans[ai - 1] = 0
for i in c:
	print(i)


(n, m) = map(int, input().split())
arr = [0] + list(map(int, input().split()))
result = arr.copy()
Map = {}
for _ in range(m):
	(ind, noOfProducts) = map(int, input().split())
	temp = list(map(int, input().split()))
	Map[ind] = temp
indexes = list(Map.keys())
indexes.sort()
for ind in indexes:
	temp = Map[ind]
	noOfProducts = len(temp) // 2
	amounts = []
	compounds = []
	for i in range(noOfProducts * 2):
		if i % 2 == 0:
			amounts.append(temp[i])
		else:
			compounds.append(temp[i])
	multiplier = result[ind]
	for i in range(noOfProducts):
		result[compounds[i]] += multiplier * amounts[i]
	result[ind] = 0
mod = 10 ** 9 + 7
for i in range(n):
	print(result[i + 1] % mod)


mod = 1000000007
(n, m) = map(int, input().split())
A = list(map(int, input().split()))
for _ in range(m):
	(c, x) = map(int, input().split())
	u = list(map(int, input().split()))
	val = A[c - 1]
	A[c - 1] = 0
	for i in range(0, len(u) - 1, 2):
		A[u[i + 1] - 1] += u[i] * val
for i in A:
	print(i % mod)


inp = input().split(' ')
n = int(inp[0])
q = int(inp[1])
MOD = 1000000007
Arr = [int(a) for a in input().split(' ')]
for i in range(q):
	inp = input().split(' ')
	c = int(inp[0]) - 1
	eq = [int(e) for e in input().split(' ')]
	for j in range(0, len(eq), 2):
		Arr[eq[j + 1] - 1] = (Arr[eq[j + 1] - 1] + eq[j] * Arr[c]) % MOD
	Arr[c] = 0
for i in range(n):
	print(Arr[i])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
for i in range(m):
	(c, x) = map(int, input().split())
	b = list(map(int, input().split()))
	for i in range(0, 2 * x, 2):
		w = b[i]
		com = b[i + 1] - 1
		a[com] += w * a[c - 1]
	a[c - 1] = 0
for i in a:
	print(i % 1000000007)