def solve(): N=int(input()) X=[None]*(N+1) for u in range(N+1): X[u]=list(map(int,input().split())) Mod=998244353 # 前計算 S=[[0]*(N+1) for _ in range(N+1)] for u in range(N+1): Su=S[u] Su[u]=1 for v in range(u,N+1): Xv=X[v] for w in range(v+1,N+1): Su[w]+=Su[v]*Xv[w] Su[w]%=Mod Q=int(input()) Ans=[0]*Q for q in range(Q): K=int(input()) A=[0]*K; B=[0]*K; C=[0]*K V=[] for k in range(K): A[k],B[k],C[k]=map(int,input().split()) V.append((A[k],1,k)) V.append((B[k],0,k)) Ans[q]=S[0][N] DPa=[S[0][A[k]] for k in range(K)] DPb=[0]*K V.sort() for v,t,k in V: if t==1: DPb[k]+=DPa[k]*C[k] DPb[k]%=Mod else: Ans[q]+=DPb[k]*S[B[k]][N] Ans[q]%=Mod for l in range(K): DPa[l]+=DPb[k]*S[B[k]][A[l]] DPa[l]%=Mod return Ans #================================================== import sys input=sys.stdin.readline write=sys.stdout.write write("\n".join(map(str,solve())))