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 N = 300500;
  const int L = 22;
  const long long M = 998244353;
  int par[N];
  int up[N][L];
  int sz[N];
  void merge(int a, int b) {
    if (sz[a] <= sz[b]) {
      par[a] = b;
      sz[b] += sz[a];
    } else {
      par[b] = a;
      sz[a] += sz[b];
    }
  }
  int parent(int a) {
    if (a == par[a]) return a;
    return par[a] = parent(par[a]);
  }
  signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    for (int i = 0; i < N; i++) {
      par[i] = i;
      sz[i] = 1;
    }
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<int>> A;
    for (int _ = 0; _ < n; _++) {
      int l;
      cin >> l;
      vector<int> B(l);
      for (int i = 0; i < l; i++) {
        cin >> B[i];
      }
      A.push_back(B);
    }
    for (auto B : A) {
      for (int i = 0; i < (int)B.size() - 1; i++) {
        merge(parent(B[i]), parent(B[i + 1]));
      }
    }
    vector<bool> ban(k + 1, false);
    vector<int> next(k + 1, -1);
    vector<int> prev(k + 1, -1);
    for (auto B : A) {
      for (int i = 0; i < (int)B.size() - 1; i++) {
        if (next[B[i]] == -1) {
          next[B[i]] = B[i + 1];
        } else if (next[B[i]] != B[i + 1]) {
          ban[parent(B[i])] = true;
        }
      }
      for (int i = 0; i < (int)B.size() - 1; i++) {
        if (prev[B[i + 1]] == -1) {
          prev[B[i + 1]] = B[i];
        } else if (prev[B[i + 1]] != B[i]) {
          ban[parent(B[i + 1])] = true;
        }
      }
      vector<int> B2 = B;
      sort(B2.begin(), B2.end());
      for (int i = 0; i < (int)B2.size() - 1; i++) {
        if (B2[i] == B2[i + 1]) {
          ban[parent(B2[i])] = true;
        }
      }
    }
    for (int i = 1; i <= k; i++) {
      if (ban[parent(i)]) {
        ban[i] = true;
      }
    }
    for (int i = 1; i <= k; i++) {
      if (!ban[i]) {
        if (next[i] != -1) {
          up[i][0] = next[i];
        } else {
          up[i][0] = i;
        }
      }
    }
    for (int l = 1; l < L; l++) {
      for (int i = 1; i <= k; i++) {
        if (!ban[i]) {
          up[i][l] = up[up[i][l - 1]][l - 1];
        }
      }
    }
    for (int i = 1; i <= k; i++) {
      if (!ban[i]) {
        if (next[up[i][L - 1]] != -1) {
          ban[i] = true;
        }
      }
    }
    vector<int> aboba;
    for (int i = 1; i <= k; i++) {
      if (!ban[i] && prev[i] == -1) {
        int j = i;
        int kek = 1;
        for (int l = L - 1; l >= 0; l--) {
          if (next[up[j][l]] != -1) {
            j = up[j][l];
            kek += (1 << l);
          }
        }
        if (next[j] != -1) {
          kek++;
        }
        aboba.push_back(kek);
      }
    }
    vector<int> boba;
    int s = (int)sqrt(n);
    vector<long long> cnt(s + 1);
    for (auto x : aboba) {
      if (x <= s) {
        cnt[x]++;
      } else {
        boba.push_back(x);
      }
    }
    vector<long long> dp(m + 1);
    dp[0] = 1;
    for (int i = 1; i <= m; i++) {
      for (int x = 1; x <= min(i, s); x++) {
        dp[i] += (cnt[x] * dp[i - x]) % M;
      }
      for (auto x : boba) {
        if (i >= x) {
          dp[i] += dp[i - x];
        }
      }
      dp[i] %= M;
    }
    cout << dp[m] << '\n';
  }
  