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;
  const int N = 105;
  int bic[1 << 16 | 5];
  void init() {
    for (int i = 0; i < 1 << 16; i++) bic[i] = bic[i >> 1] + (i & 1);
  }
  struct Bitset {
    unsigned long long a[2];
    Bitset() { a[0] = a[1] = 0; }
    void clear() { a[0] = a[1] = 0; }
    void set(int x) { a[x >> 6] |= (1ull << (x & 63)); }
    bool get(int x) const { return (a[x >> 6] >> (x & 63)) & 1; }
    Bitset operator|(const Bitset &b) const {
      Bitset c;
      c.a[0] = a[0] | b.a[0];
      c.a[1] = a[1] | b.a[1];
      return c;
    }
    Bitset operator^(const Bitset &b) const {
      Bitset c;
      c.a[0] = a[0] ^ b.a[0];
      c.a[1] = a[1] ^ b.a[1];
      return c;
    }
    Bitset operator&(const Bitset &b) const {
      Bitset c;
      c.a[0] = a[0] & b.a[0];
      c.a[1] = a[1] & b.a[1];
      return c;
    }
    int count() const {
      int ans = 0;
      ans += bic[a[0] & 65535] + bic[(a[0] >> 16) & 65535] +
             bic[(a[0] >> 32) & 65535] + bic[a[0] >> 48];
      ans += bic[a[1] & 65535] + bic[(a[1] >> 16) & 65535] +
             bic[(a[1] >> 32) & 65535] + bic[a[1] >> 48];
      return ans;
    }
    bool operator==(const Bitset &b) const {
      return a[0] == b.a[0] && a[1] == b.a[1];
    }
    bool operator!=(const Bitset &b) const {
      return a[0] != b.a[0] || a[1] != b.a[1];
    }
    operator bool() { return a[0] != 0 || a[1] != 0; }
  } B[N], C[N];
  int n, m, bad, ct;
  int vis[N], e[N][N];
  bool Judpre(int *mode) {
    int s = 0;
    for (int i = 1; i <= ct; i++)
      if (mode[i] == 1) ++s;
    if (s > 1) return 0;
    for (int i = 1; i < ct; i++)
      if (mode[i] < mode[i + 1]) return 0;
    return 1;
  }
  bool Judsuf(int *mode) {
    int s = 0;
    for (int i = 1; i <= ct; i++)
      if (mode[i] == 1) ++s;
    if (s > 1) return 0;
    for (int i = 1; i < ct; i++)
      if (mode[i] > mode[i + 1]) return 0;
    return 1;
  }
  bool Judinter(int *mode) {
    int s1 = 0;
    for (int i = 1; i <= ct; i++)
      if (mode[i] == 1) ++s1;
    if (s1 > 2) return 0;
    int l = 1, r = ct;
    for (; l != ct && mode[l] <= mode[l + 1]; ++l)
      ;
    for (; r != 1 && mode[r] <= mode[r - 1]; --r)
      ;
    if (s1 == 2 && mode[l] == 2) {
      for (int i = 1; i <= l; i++)
        if (mode[i] == 1) --s1;
      if (s1 != 1) return 0;
    }
    return l >= r;
  }
  void Insert(int loc, Bitset A) {
    ++ct;
    for (int i = ct; i > loc; i--) C[i] = C[i - 1];
    C[loc] = A;
  }
  void Insert(Bitset A) {
    Bitset Copy_A = A;
    static int mode[N];
    for (int i = 1; i <= ct; i++) {
      Bitset Int = A & C[i];
      if (Int)
        mode[i] = (Int != C[i] ? 1 : 2);
      else
        mode[i] = 0;
      A = A ^ Int;
    }
    bool pre = 0, suf = 0, inter = 0;
    pre = Judpre(mode);
    suf = Judsuf(mode);
    inter = Judinter(mode);
    if (!inter) return bad = 1, void(0);
    if (!pre && !suf && A) return bad = 1, void(0);
    if (!suf && A) {
      reverse(C + 1, C + ct + 1);
      reverse(mode + 1, mode + ct + 1);
    }
    if (A) {
      C[++ct] = A;
      mode[ct] = 2;
    }
    for (int i = ct; i >= 1; i--)
      if (mode[i] == 1) {
        Bitset b1 = C[i] & Copy_A;
        Bitset b2 = b1 ^ C[i];
        if (i >= 2 && mode[i - 1] != 0)
          C[i] = b1, Insert(i + 1, b2);
        else if (i != ct && mode[i + 1] != 0)
          C[i] = b2, Insert(i + 1, b1);
        else
          assert(0);
      }
  }
  struct Mynode {
    Bitset fi;
    vector<Bitset> se;
    bool operator<(const Mynode &a) const {
      if (fi.count() != a.fi.count()) return fi.count() > a.fi.count();
      return se.size() < a.se.size();
    }
  } node[N];
  int nodet;
  void update(int S) {
    static int q[N];
    int h = 0, t = 1;
    ct = 1;
    q[1] = S;
    vis[S] = 1;
    C[1] = B[S];
    while (h != t && !bad) {
      int x = q[++h];
      for (int i = 1; i <= m; i++)
        if (e[x][i] && !vis[i] && !bad) q[++t] = i, vis[i] = 1, Insert(B[i]);
    }
    if (bad) {
      puts("0");
      exit(0);
    }
    ++nodet;
    for (int i = 1; i <= ct; i++) {
      node[nodet].fi = node[nodet].fi | C[i];
      node[nodet].se.push_back(C[i]);
    }
  }
  int main() {
    init();
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
      int x, y;
      scanf("%d", &x);
      for (; x; --x) {
        scanf("%d", &y);
        B[i].set(y);
      }
      bool flag = 0;
      for (int j = 1; j < i; j++)
        if (B[i] == B[j]) flag = 1;
      if (flag) {
        B[i].clear();
        --i, --m;
      }
    }
    for (int i = 1; i <= m; i++)
      for (int j = i + 1; j <= m; j++)
        if (B[i] & B[j])
          if ((B[i] & B[j]) != B[i])
            if ((B[i] & B[j]) != B[j]) e[i][j] = e[j][i] = 1;
    for (int i = 1; i <= m; i++)
      if (!vis[i]) update(i);
    ++nodet;
    Bitset ful;
    for (int i = 1; i <= n; i++) ful.set(i);
    node[nodet].fi = ful;
    node[nodet].se.push_back(ful);
    sort(node + 1, node + nodet + 1);
    int ans = 1;
    int mo = 998244353;
    for (int i = 1; i <= nodet; i++) {
      if (node[i].se.size() >= 2) ans = 2ll * ans % mo;
      for (auto j : node[i].se) {
        int cnt = j.count();
        Bitset Copy_j = j;
        for (int k = i + 1; k <= nodet; k++)
          if ((node[k].fi & Copy_j) == node[k].fi) {
            Copy_j = Copy_j ^ node[k].fi;
            cnt -= node[k].fi.count() - 1;
          }
        for (; cnt; --cnt) ans = 1ll * cnt * ans % mo;
      }
    }
    printf("%d\n", ans);
  }
  