#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const unsigned gen_seed =
    std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937_64 gen(gen_seed);
long long gcd(long long a, long long b, long long& x, long long& y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long d = gcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
long long invmod(long long a, long long modulo) {
  long long x, y;
  long long g = gcd(a, modulo, x, y);
  if (g != 1) {
    return -1;
  } else {
    x = (x % modulo + modulo) % modulo;
    return x;
  }
}
std::vector<std::vector<int>> g;
std::vector<std::vector<int>> a;
std::vector<long long> imod(1000);
std::vector<long long> ifac(1000);
std::vector<long long> fac(1000);
std::vector<int> vis;
std::vector<std::vector<int>> comp;
std::vector<std::vector<int>> compv;
std::vector<std::vector<int>> touched;
void dfs(int v) {
  vis[v] = 1;
  comp.back().push_back(v);
  for (auto u : g[v]) {
    if (!vis[u]) dfs(u);
  }
}
std::vector<int> hp;
std::vector<int> ts;
int curpart = 1;
std::vector<std::vector<int>> b;
std::vector<std::vector<int>> kaka;
int n, m;
void split(int u, int v) {
  kaka = std::vector<std::vector<int>>(3);
  for (int i = 0; i < n; i++) {
    if (a[u][i] == 1) {
      if (a[v][i] == 0)
        kaka[0].push_back(i);
      else
        kaka[1].push_back(i);
    } else if (a[v][i] == 1)
      kaka[2].push_back(i);
  }
}
int can;
void addkaka(int v) {
  std::vector<int> ina(n, 0);
  for (int i = 0; i < n; i++)
    if (a[v][i]) ina[i] = 1;
  int l = kaka.size();
  std::vector<std::vector<int>> ka1(l), ka2(l);
  std::vector<int> per;
  for (int i = 0; i < kaka.size(); i++) {
    for (auto x : kaka[i]) {
      ina[x] = 0;
      if (a[v][x])
        ka1[i].push_back(x);
      else
        ka2[i].push_back(x);
    }
    if (ka1[i].size() > 0) per.push_back(i);
  }
  int y = per.size();
  for (int i = 0; i < y - 1; i++)
    if (per[i] + 1 != per[i + 1]) {
      can = 0;
      return;
    }
  for (int i = 1; i < y - 1; i++)
    if (ka2[per[i]].size() > 0) {
      can = 0;
      return;
    }
  std::vector<int> xtra;
  for (int i = 0; i < n; i++) {
    if (ina[i]) xtra.push_back(i);
  }
  if (xtra.size() > 0) {
    if (per[0] == 0 && (per.size() == 1 || ka2[0].size() == 0)) {
      kaka.clear();
      kaka.push_back(xtra);
      for (int i = 0; i < l; i++) {
        if (ka1[i].size() > 0) kaka.push_back(ka1[i]);
        if (ka2[i].size() > 0) kaka.push_back(ka2[i]);
      }
    } else if (per.back() == l - 1 &&
               (per.size() == 1 || ka2[l - 1].size() == 0)) {
      kaka.clear();
      for (int i = 0; i < l; i++) {
        if (ka2[i].size() > 0) kaka.push_back(ka2[i]);
        if (ka1[i].size() > 0) kaka.push_back(ka1[i]);
      }
      kaka.push_back(xtra);
    } else {
      can = 0;
      return;
    }
  } else {
    kaka.clear();
    for (int i = 0; i < l; i++) {
      if (i <= per[0]) {
        if (ka2[i].size() > 0) kaka.push_back(ka2[i]);
        if (ka1[i].size() > 0) kaka.push_back(ka1[i]);
      } else {
        if (ka1[i].size() > 0) kaka.push_back(ka1[i]);
        if (ka2[i].size() > 0) kaka.push_back(ka2[i]);
      }
    }
  }
}
int main() {
  long long curfac = 1;
  for (int i = 1; i < 200; i++) {
    imod[i] = invmod(i, mod);
    curfac = curfac * i % mod;
    fac[i] = curfac;
    ifac[i] = invmod(curfac, mod);
  }
  scanf("%d %d", &n, &m);
  a = std::vector<std::vector<int>>(m, std::vector<int>(n, 0));
  long long ans = fac[n];
  b.resize(m);
  for (int i = 0; i < m; i++) {
    int q;
    scanf("%d", &q);
    for (int j = 0; j < q; j++) {
      int x;
      scanf("%d", &x);
      x--;
      b[i].push_back(x);
      a[i][x] = 1;
    }
  }
  std::vector<int> ao;
  g.resize(m);
  for (int i = 0; i < m; i++)
    for (int j = i + 1; j < m; j++) {
      int can1 = 0, can2 = 0, can3 = 0;
      for (int k = 0; k < n; k++) {
        if (a[i][k] > a[j][k]) can1 = 1;
        if (a[i][k] < a[j][k]) can2 = 1;
        if (a[i][k] * a[j][k] == 1) can3 = 1;
      }
      if (can1 + can2 + can3 == 3) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  vis = std::vector<int>(m, 0);
  for (int i = 0; i < m; i++)
    if (!vis[i]) {
      comp.push_back(std::vector<int>());
      dfs(i);
    }
  int k = comp.size();
  compv.resize(k);
  vector<pair<std::pair<int, int>, int>> cord;
  for (int i = 0; i < k; i++) {
    std::vector<int> tk(n, 0);
    for (auto x : comp[i]) {
      for (int j = 0; j < n; j++) tk[j] |= a[x][j];
    }
    int sum = 0;
    for (auto x : tk) sum += x;
    cord.push_back(make_pair(make_pair(sum, -(int)comp[i].size()), i));
    for (int j = 0; j < n; j++) {
      if (tk[j]) compv[i].push_back(j);
    }
  }
  sort((cord).begin(), (cord).end());
  reverse((cord).begin(), (cord).end());
  hp = std::vector<int>(n, 0);
  ts = std::vector<int>(1005000, 0);
  ts[0] = n;
  for (auto cc : cord) {
    int ind = cc.second;
    int prt = hp[compv[ind][0]];
    int clen = cc.first.first;
    for (int i = 1; i < clen; i++)
      if (hp[compv[ind][i]] != prt) {
        ans = 0;
        break;
      }
    ans = ans * ifac[ts[prt]] % mod;
    ts[prt] -= clen - 1;
    ans = ans * fac[ts[prt]] % mod;
    if (ans == 0) break;
    if (comp[ind].size() == 1) {
      for (auto x : compv[ind]) hp[x] = curpart;
      ts[curpart] = (int)compv[ind].size();
      ans = ans * fac[ts[curpart]] % mod;
      curpart++;
      continue;
    }
    ans = ans * 2 % mod;
    can = 1;
    split(comp[ind][0], comp[ind][1]);
    for (int i = 2; i < comp[ind].size(); i++) {
      addkaka(comp[ind][i]);
      if (can == 0) break;
    }
    if (can == 0) {
      ans = 0;
      break;
    }
    for (auto x : kaka) {
      ans = ans * fac[x.size()] % mod;
      for (auto y : x) hp[y] = curpart;
      ts[curpart] = x.size();
      curpart++;
    }
  }
  cout << ans;
}

### Prompt

Create a solution in CPP for the following problem:

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.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const unsigned gen_seed =
    std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937_64 gen(gen_seed);
long long gcd(long long a, long long b, long long& x, long long& y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long d = gcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return d;
}
long long invmod(long long a, long long modulo) {
  long long x, y;
  long long g = gcd(a, modulo, x, y);
  if (g != 1) {
    return -1;
  } else {
    x = (x % modulo + modulo) % modulo;
    return x;
  }
}
std::vector<std::vector<int>> g;
std::vector<std::vector<int>> a;
std::vector<long long> imod(1000);
std::vector<long long> ifac(1000);
std::vector<long long> fac(1000);
std::vector<int> vis;
std::vector<std::vector<int>> comp;
std::vector<std::vector<int>> compv;
std::vector<std::vector<int>> touched;
void dfs(int v) {
  vis[v] = 1;
  comp.back().push_back(v);
  for (auto u : g[v]) {
    if (!vis[u]) dfs(u);
  }
}
std::vector<int> hp;
std::vector<int> ts;
int curpart = 1;
std::vector<std::vector<int>> b;
std::vector<std::vector<int>> kaka;
int n, m;
void split(int u, int v) {
  kaka = std::vector<std::vector<int>>(3);
  for (int i = 0; i < n; i++) {
    if (a[u][i] == 1) {
      if (a[v][i] == 0)
        kaka[0].push_back(i);
      else
        kaka[1].push_back(i);
    } else if (a[v][i] == 1)
      kaka[2].push_back(i);
  }
}
int can;
void addkaka(int v) {
  std::vector<int> ina(n, 0);
  for (int i = 0; i < n; i++)
    if (a[v][i]) ina[i] = 1;
  int l = kaka.size();
  std::vector<std::vector<int>> ka1(l), ka2(l);
  std::vector<int> per;
  for (int i = 0; i < kaka.size(); i++) {
    for (auto x : kaka[i]) {
      ina[x] = 0;
      if (a[v][x])
        ka1[i].push_back(x);
      else
        ka2[i].push_back(x);
    }
    if (ka1[i].size() > 0) per.push_back(i);
  }
  int y = per.size();
  for (int i = 0; i < y - 1; i++)
    if (per[i] + 1 != per[i + 1]) {
      can = 0;
      return;
    }
  for (int i = 1; i < y - 1; i++)
    if (ka2[per[i]].size() > 0) {
      can = 0;
      return;
    }
  std::vector<int> xtra;
  for (int i = 0; i < n; i++) {
    if (ina[i]) xtra.push_back(i);
  }
  if (xtra.size() > 0) {
    if (per[0] == 0 && (per.size() == 1 || ka2[0].size() == 0)) {
      kaka.clear();
      kaka.push_back(xtra);
      for (int i = 0; i < l; i++) {
        if (ka1[i].size() > 0) kaka.push_back(ka1[i]);
        if (ka2[i].size() > 0) kaka.push_back(ka2[i]);
      }
    } else if (per.back() == l - 1 &&
               (per.size() == 1 || ka2[l - 1].size() == 0)) {
      kaka.clear();
      for (int i = 0; i < l; i++) {
        if (ka2[i].size() > 0) kaka.push_back(ka2[i]);
        if (ka1[i].size() > 0) kaka.push_back(ka1[i]);
      }
      kaka.push_back(xtra);
    } else {
      can = 0;
      return;
    }
  } else {
    kaka.clear();
    for (int i = 0; i < l; i++) {
      if (i <= per[0]) {
        if (ka2[i].size() > 0) kaka.push_back(ka2[i]);
        if (ka1[i].size() > 0) kaka.push_back(ka1[i]);
      } else {
        if (ka1[i].size() > 0) kaka.push_back(ka1[i]);
        if (ka2[i].size() > 0) kaka.push_back(ka2[i]);
      }
    }
  }
}
int main() {
  long long curfac = 1;
  for (int i = 1; i < 200; i++) {
    imod[i] = invmod(i, mod);
    curfac = curfac * i % mod;
    fac[i] = curfac;
    ifac[i] = invmod(curfac, mod);
  }
  scanf("%d %d", &n, &m);
  a = std::vector<std::vector<int>>(m, std::vector<int>(n, 0));
  long long ans = fac[n];
  b.resize(m);
  for (int i = 0; i < m; i++) {
    int q;
    scanf("%d", &q);
    for (int j = 0; j < q; j++) {
      int x;
      scanf("%d", &x);
      x--;
      b[i].push_back(x);
      a[i][x] = 1;
    }
  }
  std::vector<int> ao;
  g.resize(m);
  for (int i = 0; i < m; i++)
    for (int j = i + 1; j < m; j++) {
      int can1 = 0, can2 = 0, can3 = 0;
      for (int k = 0; k < n; k++) {
        if (a[i][k] > a[j][k]) can1 = 1;
        if (a[i][k] < a[j][k]) can2 = 1;
        if (a[i][k] * a[j][k] == 1) can3 = 1;
      }
      if (can1 + can2 + can3 == 3) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  vis = std::vector<int>(m, 0);
  for (int i = 0; i < m; i++)
    if (!vis[i]) {
      comp.push_back(std::vector<int>());
      dfs(i);
    }
  int k = comp.size();
  compv.resize(k);
  vector<pair<std::pair<int, int>, int>> cord;
  for (int i = 0; i < k; i++) {
    std::vector<int> tk(n, 0);
    for (auto x : comp[i]) {
      for (int j = 0; j < n; j++) tk[j] |= a[x][j];
    }
    int sum = 0;
    for (auto x : tk) sum += x;
    cord.push_back(make_pair(make_pair(sum, -(int)comp[i].size()), i));
    for (int j = 0; j < n; j++) {
      if (tk[j]) compv[i].push_back(j);
    }
  }
  sort((cord).begin(), (cord).end());
  reverse((cord).begin(), (cord).end());
  hp = std::vector<int>(n, 0);
  ts = std::vector<int>(1005000, 0);
  ts[0] = n;
  for (auto cc : cord) {
    int ind = cc.second;
    int prt = hp[compv[ind][0]];
    int clen = cc.first.first;
    for (int i = 1; i < clen; i++)
      if (hp[compv[ind][i]] != prt) {
        ans = 0;
        break;
      }
    ans = ans * ifac[ts[prt]] % mod;
    ts[prt] -= clen - 1;
    ans = ans * fac[ts[prt]] % mod;
    if (ans == 0) break;
    if (comp[ind].size() == 1) {
      for (auto x : compv[ind]) hp[x] = curpart;
      ts[curpart] = (int)compv[ind].size();
      ans = ans * fac[ts[curpart]] % mod;
      curpart++;
      continue;
    }
    ans = ans * 2 % mod;
    can = 1;
    split(comp[ind][0], comp[ind][1]);
    for (int i = 2; i < comp[ind].size(); i++) {
      addkaka(comp[ind][i]);
      if (can == 0) break;
    }
    if (can == 0) {
      ans = 0;
      break;
    }
    for (auto x : kaka) {
      ans = ans * fac[x.size()] % mod;
      for (auto y : x) hp[y] = curpart;
      ts[curpart] = x.size();
      curpart++;
    }
  }
  cout << ans;
}

```