#include #include #include #include #include #include using namespace std; using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; int Gcd(int a, int b){ return b ? Gcd(b, a%b) : a; } int main(){ int N, M; cin >> N >> M; vector P(N); rep(i,N) P[i] = i; rep(j,M){ int n; cin >> n; vector F(n); rep(i,n){ int a; cin >> a; F[i] = a-1; } for(int i=n-2; i>=0; i--) swap(P[F[i]], P[F[i+1]]); } atcoder::dsu G(N); rep(i,N) G.merge(P[i], i); vector Q(N+1); rep(i,N) Q[G.size(i)] = 1; vector QC; rep(i,N+1) if(Q[i]){ int x = i; for(int q : QC) x /= Gcd(x, q); QC.push_back(x); } Modint ans = 1; for(auto x : QC) ans *= x; cout << ans.val() << endl; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;