mod=998244353 def cmb(n,r): if r<0 or r>n: return 0 return ((g1[n]*g2[r]%mod)*g2[n-r])%mod N=300000 g1=[1]*(N+3) for i in range(2,N+3): g1[i]=g1[i-1]*i%mod g2=[0]*len(g1) g2[-1]=pow(g1[-1],mod-2,mod) for i in range(N+1,-1,-1): g2[i]=g2[i+1]*(i+1)%mod inv=[0]*(N+3) for i in range(1,N+3): inv[i]=g2[i]*g1[i-1]%mod #X=[set() for i in range(21)] P=[21**i for i in range(10)] #X[0]=set([0]) #for i in range(20): # for j in X[i]: # for k in range(1,10): # X[i+1].add(j+P[k]) N=int(input()) S=[list(map(int,input().split())) for i in range(N)] X=[set() for i in range(N+1)] X[0]=set([0]) for i in range(N): for j in X[i]: for k in range(6): X[i+1].add(j+P[S[i][k]]) ANS=0 for x in X[N]: V=1 S=0 while x: p=x//21 q=x-p*21 S+=q V=V*g2[q]%mod x=p ANS=(ANS+g1[S]*V)%mod print(ANS)