#include using namespace std; #include using mint = atcoder::modint998244353; #include vector> prime_factorize(long long n) { vector> ret; for (long long i = 2; i * i <= n; i++) { if (n % i != 0) { continue; } int ex = 0; while (n % i == 0) { ex++; n /= i; } ret.push_back({i, ex}); } if (n != 1) { ret.push_back({n, 1}); } return ret; } int main() { int n, m; cin >> n >> m; vector p(n); iota(p.begin(), p.end(), 0); for (int i = 0; i < m; i++) { int t; cin >> t; vector s(t); for (int j = 0; j < t; j++) { cin >> s[j]; s[j]--; } int start = p[s[t - 1]]; for (int j = t - 2; j >= 0; j--) { p[s[j + 1]] = p[s[j]]; } p[s[0]] = start; } atcoder::scc_graph g(n); int ed = 0; for (int i = 0; i < n; i++) { if (i == p[i]) { continue; } ed++; g.add_edge(i, p[i]); } auto res = g.scc(); vector ps(n + 1); for (auto x : res) { auto pf = prime_factorize(x.size()); for (auto [p, e] : pf) { ps[p] = max(ps[p], e); } } mint ans = 1; for (int i = 1; i <= n; i++) { mint add = 1; for (int j = 0; j < ps[i]; j++) { add *= i; } ans *= add; } cout << ans.val() << endl; }