#include using namespace std; #include #include using namespace atcoder; using mint=modint998244353; int main(void) { int n; cin >> n; int m; cin >> m; vector t(m); vector> s(m); vector a(n); for(int i=0;i> t[i]; s[i].resize(t[i]); for(int j=0;j> s[i][j]; --s[i][j]; } int tmp=a[s[i][t[i]-1]]; for(int j=t[i]-1;j>0;--j){ a[s[i][j]]=a[s[i][j-1]]; } a[s[i][0]]=tmp; } dsu g(n); for(int i=0;i sizes; map seen; for(auto group: groups){ if(ans*gcd(ans,group.size())>=998244353 && !seen[gcd(ans,group.size())]) { seen[gcd(ans,group.size())]=true; answer*=gcd(ans,group.size()); } else if(ans*gcd(ans,group.size())<998244353) { seen[gcd(ans,group.size())]=true; ans=lcm(ans,group.size()); } } answer*=ans; cout << answer.val() << endl; return 0; }