#include #include #include using namespace std; using ll = long long; constexpr ll MOD = 998244353; struct UnionFind{ vector p; UnionFind(int n): p(vector(n, -1)){} int root(int x){ if(p[x] < 0) return x; else return p[x] = root(p[x]); } int size(int x){ return -p[root(x)]; } void unite(int x, int y){ x = root(x); y = root(y); if(x == y) return; if(size(x) < size(y)) swap(x, y); p[x] += p[y]; p[y] = x; } }; ll mygcd(ll x, ll y){ while(y != 0){ ll r = x%y; x = y; y = r; } return x; } int main(){ int n, m; cin >> n >> m; vector> s(m); for(int i = 0; i < m; i++){ int t; cin >> t; s[i] = vector(t); for(auto &it: s[i]){ cin >> it; it--; } } vector p(n); for(int i = 0; i < n; i++){ p[i] = i; } for(const auto &v: s){ int x = p[v.back()]; for(int i = v.size()-2; i >= 0; i--){ p[v[i+1]] = p[v[i]]; } p[v[0]] = x; } // for(int i = 0; i < n; i++) cerr << p[i] << " \n"[i == n-1]; UnionFind uf(n); for(int i = 0; i < n; i++) uf.unite(i, p[i]); const int lim = 2000000; vector isprime(lim); for(int i = 0; i < lim; i++) isprime[i] = i; for(int i = 2; i < lim; i++){ if(isprime[i] != i) continue; for(int j = i+i; j < lim; j += i) isprime[j] = min(isprime[j], i); } vector cnt(lim, 0); for(int i = 0; i < n; i++){ if(uf.root(i) == i){ int x = uf.size(i); // cerr << x << " "; while(x > 1){ int y = isprime[x]; int z = 0; while(x%y == 0){ z++; x /= y; } cnt[y] = max(cnt[y], z); } } } ll ans = 1; for(int i = 1; i < lim; i++){ for(int j = 0; j < cnt[i]; j++){ ans *= i; ans %= MOD; } } cout << ans << endl; return 0; }