using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, m) = (c[0], c[1]); var map = NArr(m); var p = new int[n]; for (var i = 0; i < n; ++i) p[i] = i; foreach (var repl in map) { var tmp = p[repl[repl.Length - 1] - 1]; for (var i = repl.Length - 1; i > 0; --i) { if (i > 1) { p[repl[i] - 1] = p[repl[i - 1] - 1]; } else { p[repl[i] - 1] = tmp; } } } var uf = new UnionFindTree(n); for (var i = 0; i < n; ++i) uf.Unite(i, p[i]); var dic = new Dictionary(); for (var i = 0; i < n; ++i) if (uf.GetRoot(i) == i) { foreach (var kv in PDiv(uf.GetSize(i))) { if (dic.ContainsKey(kv.Key)) dic[kv.Key] = Math.Max(dic[kv.Key], kv.Value); else dic[kv.Key] = kv.Value; } } var ans = 1L; var mod = 998_244_353; foreach (var kv in dic) for (var i = 0; i < kv.Value; ++i) ans = ans * kv.Key % mod; WriteLine(ans); } class UnionFindTree { int[] roots; public UnionFindTree(int size) { roots = new int[size]; for (var i = 0; i < size; ++i) roots[i] = -1; } public int GetRoot(int a) { if (roots[a] < 0) return a; return roots[a] = GetRoot(roots[a]); } public bool IsSameTree(int a, int b) { return GetRoot(a) == GetRoot(b); } public bool Unite(int a, int b) { var x = GetRoot(a); var y = GetRoot(b); if (x == y) return false; if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; } roots[x] += roots[y]; roots[y] = x; return true; } public int GetSize(int a) { return -roots[GetRoot(a)]; } } static Dictionary PDiv(long n) { var dic = new Dictionary(); var tmp = n; while (tmp % 2 == 0) { tmp /= 2; if (dic.ContainsKey(2)) ++dic[2]; else dic.Add(2, 1); } for (var p = 3L; p * p <= n; p += 2) { while (tmp % p == 0) { tmp /= p; if (dic.ContainsKey(p)) ++dic[p]; else dic.Add(p, 1); } if (tmp == 1) break; } if (tmp > 1) dic.Add(tmp, 1); return dic; } }