1552_I. Organizing a Music Festival

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.

Example input:
5 1
5 1 2 3 4 5

11 4
5 4 5 7 9 11
2 2 10
2 9 11
7 1 2 3 5 8 10 11

3 1
2 1 3

100 1
1 50

2 5
1 2
1 2
1 2
1 1
1 1

5 5
2 1 2
2 2 3
2 3 4
2 4 5
2 1 5


Example output:
120

384

4

35305197

2

0


Tags: dfs and similar
math
Difficulty rating: 3400

Solution:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110, P = 998244353;
int n, m, vis[maxn];
bitset<maxn> b[maxn];
vector<int> G[maxn];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1, q; i <= m; i++) {
    scanf("%d", &q);
    while (q--) {
      int p;
      scanf("%d", &p);
      b[i][p] = 1;
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = i + 1; j <= m; j++) {
      auto o = b[i] & b[j];
      if (o.any() && o != b[i] && o != b[j]) {
        G[i].push_back(j), G[j].push_back(i);
      }
    }
  }
  function<int(int, vector<bitset<maxn>> &)> solve =
      [&](int v, vector<bitset<maxn>> &E) {
        vis[v] = 1;
        string inter(E.size(), ' ');
        auto a = b[v];
        for (int i = 0; i < E.size(); i++) {
          auto o = a & E[i];
          if (o.none())
            inter[i] = '0';
          else if (o != E[i])
            inter[i] = '1';
          else
            inter[i] = '2';
          a &= ~o;
        }
        bool pre = regex_match(inter, regex("2*1?0*"));
        bool suf = regex_match(inter, regex("0*1?2*"));
        bool interval = regex_match(inter, regex("0*1?2*1?0*"));
        if (!interval) return 0;
        if (!pre && !suf && a.any()) return 0;
        if (!suf && a.any()) {
          reverse(inter.begin(), inter.end());
          reverse(E.begin(), E.end());
        }
        if (a.any()) E.push_back(a), inter.push_back('2');
        for (int i = E.size() - 1; ~i; i--)
          if (inter[i] == '1') {
            auto o1 = b[v] & E[i], o2 = ~b[v] & E[i];
            E.erase(E.begin() + i);
            if (i && inter[i - 1] ^ '0') swap(o1, o2);
            E.insert(E.begin() + i, o1);
            E.insert(E.begin() + i, o2);
          }
        for (int u : G[v])
          if (!vis[u]) {
            if (!solve(u, E)) return 0;
          }
        return 1;
      };
  vector<pair<bitset<maxn>, vector<bitset<maxn>>>> eq;
  for (int i = 1; i <= m; i++)
    if (!vis[i]) {
      vector<bitset<maxn>> E;
      if (!solve(i, E)) puts("0"), exit(0);
      bitset<maxn> o;
      for (auto &e : E) o |= e;
      eq.emplace_back(o, E);
    }
  bitset<maxn> all;
  for (int i = 1; i <= n; i++) all[i] = 1;
  eq.emplace_back(all, vector<bitset<maxn>>{all});
  sort(eq.begin(), eq.end(),
       [&](pair<bitset<maxn>, vector<bitset<maxn>>> a,
           pair<bitset<maxn>, vector<bitset<maxn>>> b) {
         return make_pair(-a.first.count(), a.second.size()) <
                make_pair(-b.first.count(), b.second.size());
       });
  int ans = 1;
  for (int i = 0; i < eq.size(); i++) {
    if (eq[i].second.size() > 1) (ans *= 2) %= P;
    for (auto o : eq[i].second) {
      int cnt = o.count();
      for (int j = i + 1; j < eq.size(); j++) {
        auto p = eq[j].first;
        if ((o & p) == p) cnt -= p.count() - 1, o &= ~p;
      }
      for (int j = 1; j <= cnt; j++) ans = 1LL * ans * j % P;
    }
  }
  printf("%d\n", ans);
  return 0;
}