description: A subarray of array a from index l to the index r is the array [a_l, a_{l+1}, ..., a_{r}]. The number of occurrences of the array b in the array a is the number of subarrays of a such that they are equal to b.
  
  You are given n arrays A_1, A_2, ..., A_n; the elements of these arrays are integers from 1 to k. You have to build an array a consisting of m integers from 1 to k in such a way that, for every given subarray A_i, the number of occurrences of A_i in the array a is not less than the number of occurrences of each non-empty subarray of A_i in a. Note that if A_i doesn't occur in a, and no subarray of A_i occurs in a, this condition is still met for A_i.
  
  Your task is to calculate the number of different arrays a you can build, and print it modulo 998244353.
  
  Input
  
  The first line contains three integers n, m and k (1 ≤ n, m, k ≤ 3 ⋅ 10^5) — the number of the given arrays, the desired length of the array a, and the upper bound on the values in the arrays.
  
  Then n lines follow. The i-th line represents the array A_i. The first integer in the i-th line is c_i (1 ≤ c_i ≤ m) — the number of elements in A_i; then, c_i integers from 1 to k follow — the elements of the array A_i.
  
  Additional constraint on the input: ∑_{i=1}^n c_i ≤ 3 ⋅ 10^5; i. e., the number of elements in the given arrays in total does not exceed 3 ⋅ 10^5.
  
  Output
  
  Print one integer — the number of different arrays a you can build, taken modulo 998244353.
  
  Examples
  
  Input
  
  
  2 4 3
  2 1 2
  1 3
  
  
  Output
  
  
  5
  
  
  Input
  
  
  2 4 3
  2 1 2
  3 3 2 1
  
  
  Output
  
  
  0
  
  
  Input
  
  
  1 42 1337
  2 13 31
  
  
  Output
  
  
  721234447
solution: #include <bits/stdc++.h>
  using namespace std;
  const int mod = 998244353, G = 3;
  void ADD(int &x, int y) {
    x += y;
    if (x >= mod) x -= mod;
  }
  void SUB(int &x, int y) {
    x -= y;
    if (x < 0) x += mod;
  }
  long long power(long long a, int b) {
    long long ans = 1;
    while (b) {
      if (b & 1) ans = ans * a % mod;
      a = a * a % mod, b >>= 1;
    }
    return ans;
  }
  namespace PolyOp {
  int t1[1 << 20], t2[1 << 20], num[1 << 20];
  long long W[1 << 20 | 10];
  int LSTN;
  long long invn;
  void PreCalcNTT(int N) {
    if (N == LSTN) return;
    LSTN = N, invn = power(N, mod - 2);
    for (int i = 0; i < N; i++) {
      num[i] = num[i >> 1] >> 1 | (i & 1 ? N >> 1 : 0);
    }
    W[0] = 1, W[1] = power(G, (mod - 1) / N);
    for (int i = 2; i <= N; i++) W[i] = W[i - 1] * W[1] % mod;
  }
  void NTT(int *f, int N, int flag) {
    for (int i = 0; i < N; i++) {
      if (i < num[i]) swap(f[i], f[num[i]]);
    }
    for (int len = 2; len <= N; len <<= 1) {
      for (int i = 0; i < N; i += len) {
        int *fl = f + i, *fr = fl + len / 2, step = flag ? -N / len : N / len;
        long long *w = flag ? W + N : W;
        for (int j = 0; j < len / 2; j++) {
          int tmp = *fr * *w % mod;
          SUB(*fr = *fl, tmp), ADD(*fl, tmp);
          fl++, fr++, w += step;
        }
      }
    }
    if (flag) {
      for (int i = 0; i < N; i++) {
        f[i] = f[i] * invn % mod;
      }
    }
  }
  void polyinv(int *f, int n, int *g) {
    if (n == 1) {
      g[0] = power(f[0], mod - 2);
      return;
    }
    int mid = n + 1 >> 1;
    polyinv(f, mid, g);
    int N = 1;
    while (N < mid + mid + n - 2) N <<= 1;
    PreCalcNTT(N);
    for (int i = 0; i < N; i++) {
      t1[i] = i < mid ? g[i] : 0, t2[i] = i < n ? f[i] : 0;
    }
    NTT(t1, N, 0), NTT(t2, N, 0);
    for (int i = 0; i < N; i++) {
      t1[i] = 1ll * t1[i] * t1[i] % mod * t2[i] % mod;
    }
    NTT(t1, N, 1);
    for (int i = 0; i < n; i++) {
      g[i] = (2ll * (i < mid ? g[i] : 0) - t1[i] + mod) % mod;
    }
  }
  }  // namespace PolyOp
  int n, m, k;
  vector<vector<int> > a;
  int cnt[300010];
  int f[300010], g[300010];
  int suc[300010], pre[300010], vis[300010], VIS = 2333;
  int main() {
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < n; i++) {
      int k;
      scanf("%d", &k);
      int lst = -1;
      while (k--) {
        int x;
        scanf("%d", &x);
        if (lst != -1) {
          if (suc[lst] == 0)
            suc[lst] = x;
          else if (suc[lst] != x)
            suc[lst] = -1;
          if (pre[x] == 0)
            pre[x] = lst;
          else if (pre[x] != lst)
            pre[x] = -1;
        }
        lst = x;
      }
    }
    f[0] = 1;
    for (int i = 1; i <= k; i++) {
      if (pre[i] == 0) {
        VIS++;
        int cur = i, cnt = 1;
        vis[cur] = VIS;
        while (1) {
          if (suc[cur] == -1) goto BAD;
          if (suc[cur] == 0) break;
          cur = suc[cur], cnt++;
          if (vis[cur] == VIS) goto BAD;
          if (pre[cur] == -1) goto BAD;
          vis[cur] = VIS;
        }
        f[cnt]--;
      BAD:;
      }
    }
    for (int i = 1; i <= m; i++) {
      f[i] = (f[i] + mod) % mod;
    }
    PolyOp ::polyinv(f, m + 1, g);
    printf("%d\n", g[m]);
    return 0;
  }
  