mod=998244353 fac=[1] for i in range(1,100): fac.append(fac[-1]*i%mod) finv=[] for i in range(100): finv.append(pow(fac[i],mod-2,mod)) n=int(input()) s=[] for i in range(n): s.append(list(map(lambda x:int(x)-1,input().split()))) S=set([0]) for si in s: nS=set() for i in S: for j in si: nS.add(i+(1<<(5*j))) S=nS ans=0 for bit in S: res=fac[n] for j in range(9): x=bit&31 res*=finv[x] res%=mod bit>>=5 ans+=res print(ans%mod)