#include #include using mint = atcoder::modint998244353; using namespace std; int main() { int N, M; cin >> N >> M; vector A(N); iota(A.begin(), A.end(), 0); for (int i = 0; i < M; i++){ int T; cin >> T; vector S(T); for (int j = 0; j < T; j++){ cin >> S[j]; S[j]--; } vector B(T); for (int j = 0; j < T; j++){ B[j] = A[S[j]]; } for (int j = 0; j < T; j++){ A[S[j]] = B[(j - 1 + T) % T]; } } atcoder::dsu uf(N); for (int i = 0; i < N; i++){ uf.merge(i, A[i]); } mint ans = 1; set S; for (auto s : uf.groups()){ S.insert(s.size()); } for (int i : S){ ans *= i; } cout << ans.val() << '\n'; }