#include namespace nono { constexpr long long mod = 998244353; long long pow(long long base, long long exp, long long mod) { long long result = 1; while (exp > 0) { if (exp & 1) { result *= base; result %= mod; } base *= base; base %= mod; exp >>= 1; } return result; } void solve() { int n, m; std::cin >> n >> m; std::vector a(n); std::iota(a.begin(), a.end(), 0); for (int i = 0; i < m; i++) { int t; std::cin >> t; std::vector s(t); for (int j = 0; j < t; j++) { std::cin >> s[j]; s[j]--; } int temp = a[s.back()]; for (int j = t - 1; j > 0; j--) { a[s[j]] = a[s[j - 1]]; } a[s.front()] = temp; } // for (int i = 0; i < n; i++) { // std::cout << a[i] << ' '; // } // std::cout << std::endl; std::vector used(n); std::vector cycle; for (int i = 0; i < n; i++) { if (not used[i]) { int s = i; int len = 0; while (not used[s]) { used[s] = true; s = a[s]; len++; } if (len == 1) continue; cycle.push_back(len); } } long long ans = 1; long long gcd = 0; for (auto v: cycle) { ans *= v; ans %= mod; gcd = std::gcd(gcd, v); } // std::cout << ans << ' ' << gcd << std::endl; if ((int)cycle.size() == 0) { std::cout << 1 << std::endl; } else if ((int)cycle.size() == 1) { std::cout << ans << std::endl; } else { std::cout << (ans * pow(gcd, mod - 2, mod) % mod) << std::endl; } } } // namespace nono int main() { std::cin.tie(0)->sync_with_stdio(0); std::cout << std::fixed << std::setprecision(15); int t = 1; // std::cin >> t; while (t--) nono::solve(); }