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)] F=[(i in S[-1]) for i in range(10)] X=[set() for i in range(N+1)] X[0]=set([0]) for i in range(N-1): for j in X[i]: for k in range(6): X[i+1].add(j+P[S[i][k]]) ANS=0 for a in range(N+1): for b in range(a,N+1): for c in range(b,N+1): for d in range(c,N+1): for e in range(d,N+1): for f in range(e,N+1): for g in range(f,N+1): for h in range(g,N+1): i=N x=[0,a,b,c,d,e,f,g,h,i] j=0 V=g1[N] for k in range(8,-1,-1): x[k+1]-=x[k] j+=P[k+1]*x[k+1] V=V*g2[x[k+1]]%mod for k in range(9): if x[k+1]>0 and (j-P[k+1] in X[N-1]) and F[k+1]: ANS+=V break print(ANS)