import java.util.ArrayList; import java.util.BitSet; import java.util.Scanner; public class Main { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { int N = sc.nextInt(); int[][] S = new int[N][6]; for (int i = 0;i < N;++ i) for (int j = 0;j < 6;++ j) S[i][j] = sc.nextInt() - 1; long[] factorial = new long[N + 1]; factorial[0] = 1; for (int i = 1;i < factorial.length;++ i) factorial[i] = factorial[i - 1] * i; BitSet checkUnique = new BitSet(1 << N + 9); // 64MB程度 ArrayList nowQueue = new ArrayList<>(3200000), nextQueue = new ArrayList<>(3200000); nowQueue.add(new DiceSet(0b111111111, 0b01001_01000_00111_00110_00101_00100_00011_00010_00001_00000L)); // 初項M_0を求める for (int[] dice : S) { for (DiceSet multiSet : nowQueue) multiSet.next(dice, checkUnique, nextQueue); // M_iからM_{i+1}を求める ArrayList swap = nowQueue; nowQueue = nextQueue; nextQueue = swap; nextQueue.clear(); } int ans = 0; for (DiceSet multiSet : nowQueue) ans = (int)((ans + multiSet.multichoose(factorial)) % 998_244_353); System.out.println(ans); } } /** * ダイスから作られる整数の多重集合を管理するクラスです。 * @param multiSet 多重集合を、仕切りの考え方で見なした時のbit列 * @param partition multiSetで立っているbitの位置、5bitごとに管理 */ static record DiceSet(int multiSet, long partition) { int getPartition(int index) { // multiSetでindex番目に立っているbitの位置を求める return (int)(partition >> 5 * index & 0b11111); } long multichoose(long[] factorial) { // この多重集合を並べてできる組合せ long ans = factorial[getPartition(9) - 9]; for (int i = 0;i < 9;++ i) ans /= factorial[getPartition(i + 1) - getPartition(i) - 1]; return ans; } void next(int[] dice, BitSet checkUnique, ArrayList nextQueue) { // diceを追加したときにできる新たな多重集合のうち、新しく発見したものをnextQueueに入れる for (int result : dice) { int mask = (1 << getPartition(result)) - 1; int nextSet = (multiSet & ~mask) << 1 | (multiSet & mask); if (checkUnique.get(nextSet)) continue; checkUnique.set(nextSet); long nextPartition = partition + (0b00001_00001_00001_00001_00001_00001_00001_00001_00001_00001L & ~((0x20L << result * 5) - 1)); nextQueue.add(new DiceSet(nextSet, nextPartition)); } } } }