#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 998244353;
struct Mint {
  int val;
  Mint() { val = 0; }
  Mint(ll x) {
    val = (-MOD <= x && x < MOD) ? x : x % MOD;
    if (val < 0) val += MOD;
  }
  template <typename U>
  explicit operator U() const {
    return (U)val;
  }
  friend bool operator==(const Mint& a, const Mint& b) {
    return a.val == b.val;
  }
  friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
  friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; }
  Mint& operator+=(const Mint& m) {
    if ((val += m.val) >= MOD) val -= MOD;
    return *this;
  }
  Mint& operator-=(const Mint& m) {
    if ((val -= m.val) < 0) val += MOD;
    return *this;
  }
  Mint& operator*=(const Mint& m) {
    val = (ll)val * m.val % MOD;
    return *this;
  }
  friend Mint modex(Mint a, ll p) {
    assert(p >= 0);
    Mint ans = 1;
    for (; p; p >>= 1, a *= a)
      if (p & 1) ans *= a;
    return ans;
  }
  Mint& operator/=(const Mint& m) { return *this *= modex(m, MOD - 2); }
  Mint& operator++() { return *this += 1; }
  Mint& operator--() { return *this -= 1; }
  Mint operator++(int) {
    Mint result(*this);
    *this += 1;
    return result;
  }
  Mint operator--(int) {
    Mint result(*this);
    *this -= 1;
    return result;
  }
  Mint operator-() const { return Mint(-val); }
  friend Mint operator+(Mint a, const Mint& b) { return a += b; }
  friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
  friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
  friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
  friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; }
  friend string to_string(const Mint& b) { return to_string(b.val); }
};
vector<Mint> fac(1, 1), invfac(1, 1);
Mint binom(int n, int k) {
  if (k < 0 || k > n) return 0;
  while (fac.size() <= n) {
    fac.push_back(fac.back() * fac.size());
    invfac.push_back(1 / fac.back());
  }
  return fac[n] * invfac[k] * invfac[n - k];
}
const int N = 500 + 5;
struct PqTree {
  int n, tot;
  int fail;
  vector<int> G[N << 2];
  int ty[N << 2];
  int sz[N << 2];
  int szc[N << 2];
  bool s[N];
  inline int getstate(int u) {
    if (szc[u] == 0) return 0;
    if (szc[u] == sz[u]) return 2;
    return 1;
  }
  void addson(int x, int y) {
    if (y) G[x].push_back(y);
  }
  void join(int x, int y) {
    for (auto v : G[y]) G[x].push_back(v);
  }
  int mergeP(vector<int>& vec) {
    if (vec.size() == 0) return 0;
    if (vec.size() == 1) return vec[0];
    G[++tot] = vec;
    return tot;
  }
  void init(int _n) {
    n = _n;
    tot = n + 1;
    memset(ty, 0, sizeof(ty));
    for (int i = 1; i <= n; ++i) G[n + 1].push_back(i);
    fail = 0;
  }
  void dfs(int u) {
    sz[u] = u <= n;
    szc[u] = u <= n && s[u];
    for (auto v : G[u]) {
      dfs(v);
      sz[u] += sz[v];
      szc[u] += szc[v];
    }
  }
  int check(int u, int t) {
    if (fail) return 0;
    vector<int> vec[3];
    for (auto v : G[u]) vec[getstate(v)].push_back(v);
    if (vec[1].size() > 2 || (t && vec[1].size() > 1)) return fail = 1, 0;
    if (t == 0 && vec[1].size() == 1 && vec[2].size() == 0)
      return check(vec[1][0], 0);
    if (ty[u] == 0) {
      int p2 = mergeP(vec[2]);
      if (t == 0) {
        G[u] = vec[0];
        if (vec[1].size() == 0)
          addson(u, p2);
        else {
          int tmp1 = check(vec[1][0], 2);
          addson(tmp1, p2);
          if (vec[1].size() == 2) join(tmp1, check(vec[1][1], 1));
          addson(u, tmp1);
        }
        return u;
      }
      ty[u] = 1;
      G[u].clear();
      addson(u, p2);
      if (vec[1].size() == 1) join(u, check(vec[1][0], 1));
      addson(u, mergeP(vec[0]));
      if (t == 2) reverse(G[u].begin(), G[u].end());
      return u;
    }
    if (getstate(G[u].front()) > getstate(G[u].back()))
      reverse(G[u].begin(), G[u].end());
    int flag = 0;
    vector<int> tG;
    for (auto v : G[u]) {
      int sta = getstate(v);
      if (sta == 0) {
        if (flag == 1) flag = 2;
        tG.push_back(v);
      } else if (sta == 2) {
        if (flag == 0)
          flag = 1;
        else if (flag == 2)
          return fail = 2, 0;
        tG.push_back(v);
      } else {
        if (flag == 0)
          flag = 1, check(v, 2);
        else if (flag == 1)
          flag = 2, check(v, 1);
        else
          return fail = 3, 0;
        for (auto x : G[v]) tG.push_back(x);
      }
    }
    if (t && flag == 2) return fail = 4, 0;
    if (t == 1) reverse(tG.begin(), tG.end());
    G[u] = tG;
    return u;
  }
  void dfsPermutation(int u, vector<int>& per) {
    if (u <= n) per.push_back(u);
    for (auto v : G[u]) dfsPermutation(v, per);
  }
  vector<int> getPermutation() {
    vector<int> res;
    dfsPermutation(n + 1, res);
    return res;
  }
  Mint dfsCount(int u) {
    if (u <= n) return 1;
    Mint ret = 1;
    if (!ty[u]) {
      ret *= fac[G[u].size()];
    } else if (G[u].size() != 1) {
      ret *= 2;
    }
    for (auto v : G[u]) ret *= dfsCount(v);
    return ret;
  }
  Mint countPermutations() {
    binom(n, 0);
    return dfsCount(n + 1);
  }
  void restrict(const vector<int>& res) {
    for (int i = 1; i <= n; ++i) s[i] = 0;
    for (auto x : res) s[x] = 1;
    dfs(n + 1);
    check(n + 1, 0);
  }
};
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  PqTree pq;
  pq.init(n);
  for (int i = 0; i < m; ++i) {
    int q;
    scanf("%d", &q);
    vector<int> res;
    while (q--) {
      int s;
      scanf("%d", &s);
      res.push_back(s);
    }
    pq.restrict(res);
  }
  if (pq.fail) {
    printf("0\n");
  } else {
    Mint ans = pq.countPermutations();
    printf("%d\n", ans.val);
  }
}
