#include using namespace std; #include using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint998244353; using P = pair; using lb = long double; using T = tuple; #ifdef LOCAL # include # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast(0)) #endif int main() { int n, m; cin >> n >> m; vector a(n); rep(i,n) a[i] = i; rep(i,m){ int t; cin >> t; vector b(t); rep(j,t){ cin >> b[j]; --b[j]; } reverse(b.begin(),b.end()); rep(j,t-1){ swap(a[b[j]], a[b[j+1]]); } } scc_graph g(n); rep(i,n){ g.add_edge(i, a[i]); } dbg(a); auto res = g.scc(); map mp; for(auto vs : res){ int v = vs.size(); for(int r=2;r*r<=v;r++){ if(v%r==0){ int cnt = 0; while(v%r==0) { v /=r; cnt++; } mp[r] = max(mp[r], cnt); } } if(v!=1){ mp[v] = max(mp[v], 1); } } mint ans = 1; for(auto p : mp){ ans *= mint(p.first).pow(p.second); } cout << ans.val() << endl; return 0; }