#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } using mint = atcoder::modint998244353; ostream& operator<<(ostream& os, const mint& m){ os << m.val(); return os; } class Eratosthenes{ public: int m; vector is_prime; vector primes; Eratosthenes(int m_){ m = m_; init(); } private: void init(){ is_prime.assign(m+1, true); is_prime[0] = false, is_prime[1] = false; for(int i = 2; i <= m; i++){ if(is_prime[i]){ primes.push_back(i); for(int j = 2; i*j <= m; j++) is_prime[i*j] = false; } } } }; struct UnionFind { vector data; UnionFind(int size) : data(size, -1) {} bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; const int N = 1000000; using P = pair; vector

facs[N+1]; auto et = Eratosthenes(N); void init(){ for(int p: et.primes){ for(int i = 1; i*p <= N; i++){ int x = i*p; int c = 0; while(x%p == 0){ c++; x /= p; } facs[i*p].push_back({p, c}); } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; init(); int n, m; cin >> n >> m; vector> s(m); vector a(n); iota(a.begin(), a.end(), 0); for(int i = 0; i < m; i++){ int t; cin >> t; s[i].resize(t); for(int j = 0; j < t; j++){ cin >> s[i][j]; s[i][j]--; } reverse(s[i].begin(),s[i].end()); int a0 = a[s[i][0]]; for(int j = 0; j < t-1; j++){ a[s[i][j]] = a[s[i][j+1]]; } a[s[i].back()] = a0; } UnionFind uf(n); for(int i = 0; i < n; i++) uf.unionSet(i, a[i]); mint ans = 1; vector mx(N+1); for(int i = 0; i < n; i++){ if(uf.root(i) == i) { for(auto [p, c]: facs[uf.size(i)]){ chmax(mx[p], c); } } } for(int x = 0; x <= N; x++){ ans *= mint(x).pow(mx[x]); } cout << ans << endl; }