from collections import deque
import heapq
import copy
mod=998244353
n=int(input())
Edge=[list(map(int,input().split())) for _ in range(n+1)]
Q=int(input())
for _ in range(Q):
  E=copy.deepcopy(Edge)
  K=int(input())
  for _ in range(K):
    a,b,c=map(int,input().split())
    E[a][b]+=c
  tmp=[0]*(n+1)
  tmp[0]=1
  q=[]
  heapq.heappush(q,0)
  while q:
    x=heapq.heappop(q)
    for i in range(x+1,n+1):
      if 0<E[x][i]:
        tmp[i]+=tmp[x]*E[x][i]
        tmp[i]%=mod
        heapq.heappush(q,i)
  print(tmp[-1]%mod)