description: You are the organizer of the famous "Zurich Music Festival". There will be n singers who will perform at the festival, identified by the integers 1, 2, ..., n. You must choose in which order they are going to perform on stage. 
  
  You have m friends and each of them has a set of favourite singers. More precisely, for each 1≤ i≤ m, the i-th friend likes singers s_{i,1},   s_{i, 2},   ..., \,s_{i, q_i}.
  
  A friend of yours is happy if the singers he likes perform consecutively (in an arbitrary order). An ordering of the singers is valid if it makes all your friends happy.
  
  Compute the number of valid orderings modulo 998 244 353.
  
  Input
  
  The first line contains two integers n and m (1≤ n,\,m≤ 100) — the number of singers and the number of friends correspondingly.
  
  The i-th of the next m lines contains the integer q_i (1≤ q_i≤ n) — the number of favorite singers of the i-th friend – followed by the q_i integers s_{i,1},   s_{i, 2},   ..., \,s_{i, q_i} (1≤ s_{i,1}<s_{i,2}<⋅⋅⋅<s_{i,q_i}≤ n) — the indexes of his favorite singers.
  
  Output
  
  Print the number of valid orderings of the singers modulo 998 244 353.
  
  Examples
  
  Input
  
  
  3 1
  2 1 3
  
  
  Output
  
  
  4
  
  
  Input
  
  
  5 5
  2 1 2
  2 2 3
  2 3 4
  2 4 5
  2 1 5
  
  
  Output
  
  
  0
  
  
  Input
  
  
  100 1
  1 50
  
  
  Output
  
  
  35305197
  
  
  Input
  
  
  5 1
  5 1 2 3 4 5
  
  
  Output
  
  
  120
  
  
  Input
  
  
  2 5
  1 2
  1 2
  1 2
  1 1
  1 1
  
  
  Output
  
  
  2
  
  
  Input
  
  
  11 4
  5 4 5 7 9 11
  2 2 10
  2 9 11
  7 1 2 3 5 8 10 11
  
  
  Output
  
  
  384
  
  Note
  
  Explanation of the first sample: There are 3 singers and only 1 friend. The friend likes the two singers 1 and 3. Thus, the 4 valid orderings are: 
  
    * 1 3 2
    * 2 1 3
    * 2 3 1
    * 3 1 2
  
  
  
  Explanation of the second sample: There are 5 singers and 5 friends. One can show that no ordering is valid.
  
  Explanation of the third sample: There are 100 singers and only 1 friend. The friend likes only singer 50, hence all the 100! possible orderings are valid.
  
  Explanation of the fourth sample: There are 5 singers and only 1 friend. The friend likes all the singers, hence all the 5!=120 possible orderings are valid.
solution: #include <bits/stdc++.h>
  using namespace std;
  long long get_time() {
    return chrono::duration_cast<chrono::nanoseconds>(
               chrono::steady_clock::now().time_since_epoch())
        .count();
  }
  template <typename T1, typename T2>
  string print_iterable(T1 begin_iter, T2 end_iter, int counter) {
    bool done_something = false;
    stringstream res;
    res << "[";
    for (; begin_iter != end_iter and counter; ++begin_iter) {
      done_something = true;
      counter--;
      res << *begin_iter << ", ";
    }
    string str = res.str();
    if (done_something) {
      str.pop_back();
      str.pop_back();
    }
    str += "]";
    return str;
  }
  template <typename S, typename T>
  ostream& operator<<(ostream& out, const pair<S, T>& p) {
    out << "{" << p.first << ", " << p.second << "}";
    return out;
  }
  template <typename T>
  ostream& operator<<(ostream& out, const vector<T>& v) {
    out << "[";
    for (int i = 0; i < (int)v.size(); i++) {
      out << v[i];
      if (i != (int)v.size() - 1) out << ", ";
    }
    out << "]";
    return out;
  }
  template <class TH>
  void _dbg(const char* name, TH val) {
    clog << name << ": " << val << endl;
  }
  template <class TH, class... TA>
  void _dbg(const char* names, TH curr_val, TA... vals) {
    while (*names != ',') clog << *names++;
    clog << ": " << curr_val << ", ";
    _dbg(names + 1, vals...);
  }
  const long long mod = 998244353;
  const int MAXN = 100;
  const int MAXM = 100;
  int N;
  bool visited[MAXM];
  vector<int> aa[MAXM];
  int M;
  bitset<MAXN> S[MAXM];
  bool dfs(int v, vector<bitset<MAXN>>& E) {
    visited[v] = true;
    string intersection_type(E.size(), '-');
    bitset<MAXN> A = S[v];
    for (int i = 0; i < ((int)((E).size())); i++) {
      bitset<MAXN> inter = A & E[i];
      if (inter.none())
        intersection_type[i] = '0';
      else if (inter != E[i])
        intersection_type[i] = '1';
      else
        intersection_type[i] = '2';
      A = A & ~inter;
    }
    bool is_prefix = regex_match(intersection_type, regex("2*1?0*"));
    bool is_suffix = regex_match(intersection_type, regex("0*1?2*"));
    bool is_interval = regex_match(intersection_type, regex("0*1?2*1?0*"));
    if (!is_interval) return false;
    if (!is_prefix and !is_suffix and A.any()) return false;
    if (!is_suffix and A.any()) {
      reverse(E.begin(), E.end());
      reverse(intersection_type.begin(), intersection_type.end());
    }
    if (A.any()) {
      E.push_back(A);
      intersection_type += '2';
    }
    for (int i = ((int)((E).size())) - 1; i >= 0; i--) {
      if (intersection_type[i] == '1') {
        bitset<MAXN> e1 = S[v] & E[i];
        bitset<MAXN> e2 = E[i] & ~S[v];
        E.erase(E.begin() + i);
        if (i >= 1 and intersection_type[i - 1] != '0') {
          E.insert(E.begin() + i, e2);
          E.insert(E.begin() + i, e1);
        } else if (i < ((int)((intersection_type).size())) - 1 and
                   intersection_type[i + 1] != '0') {
          E.insert(E.begin() + i, e1);
          E.insert(E.begin() + i, e2);
        } else
          assert(0);
      }
    }
    for (int a : aa[v]) {
      if (visited[a]) continue;
      if (!dfs(a, E)) return false;
    }
    return true;
  }
  long long solve() {
    for (int i = 0; i < M; i++) aa[i].clear(), visited[i] = false;
    for (int i = 0; i < M; i++) {
      for (int j = i + 1; j < M; j++) {
        bitset<MAXN> inter = S[i] & S[j];
        if (inter.any() and inter != S[i] and inter != S[j]) {
          aa[i].push_back(j);
          aa[j].push_back(i);
        }
      }
    }
    vector<pair<bitset<MAXN>, vector<bitset<MAXN>>>> equiv;
    for (int i = 0; i < M; i++) {
      if (visited[i]) continue;
      vector<bitset<MAXN>> E;
      if (!dfs(i, E)) return 0;
      bitset<MAXN> PP;
      for (auto e : E) PP |= e;
      equiv.push_back({PP, E});
    }
    bitset<MAXN> full_set;
    for (int i = 0; i < N; i++) full_set[i] = 1;
    equiv.emplace_back(full_set, vector<bitset<MAXN>>{full_set});
    sort(equiv.begin(), equiv.end(),
         [&](const pair<bitset<MAXN>, vector<bitset<MAXN>>>& A,
             const pair<bitset<MAXN>, vector<bitset<MAXN>>>& B) {
           if (A.first.count() != B.first.count())
             return A.first.count() > B.first.count();
           return A.second.size() < B.second.size();
         });
    long long res = 1;
    for (int i = 0; i < ((int)((equiv).size())); i++) {
      if (equiv[i].second.size() >= 2) res *= 2;
      for (bitset<MAXN> A : equiv[i].second) {
        int cnt = A.count();
        for (int j = i + 1; j < ((int)((equiv).size())); j++) {
          bitset<MAXN> P = equiv[j].first;
          if ((P & A) == P) {
            cnt -= P.count() - 1;
            A &= ~P;
          }
        }
        for (int m = 1; m <= cnt; m++) res = res * m % mod;
      }
    }
    return res;
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> N >> M;
    for (int i = 0; i < M; i++) {
      int q;
      cin >> q;
      for (int j = 0; j < q; j++) {
        int s;
        cin >> s;
        S[i][s - 1] = 1;
      }
    }
    cout << solve() << "\n";
  }
  