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 long long MOD = 998244353;
  int main() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> c(n);
    vector<vector<int>> A(n);
    for (int i = 0; i < n; i++) {
      cin >> c[i];
      A[i].resize(c[i]);
      for (int j = 0; j < c[i]; j++) {
        cin >> A[i][j];
        A[i][j]--;
      }
    }
    vector<vector<int>> E1(k), E2(k);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < c[i] - 1; j++) {
        E1[A[i][j]].push_back(A[i][j + 1]);
        E2[A[i][j + 1]].push_back(A[i][j]);
      }
    }
    for (int i = 0; i < k; i++) {
      sort(E1[i].begin(), E1[i].end());
      E1[i].erase(unique(E1[i].begin(), E1[i].end()), E1[i].end());
      sort(E2[i].begin(), E2[i].end());
      E2[i].erase(unique(E2[i].begin(), E2[i].end()), E2[i].end());
    }
    vector<bool> used(k, false);
    vector<int> S;
    for (int i = 0; i < k; i++) {
      if (!used[i] && E2[i].empty()) {
        used[i] = true;
        int v = i;
        int cnt = 1;
        bool ok = true;
        while (true) {
          if (E1[v].empty()) {
            break;
          }
          if (E1[v].size() >= 2) {
            ok = false;
            break;
          }
          int w = E1[v][0];
          if (E2[w].size() >= 2) {
            ok = false;
            break;
          }
          if (used[w]) {
            ok = false;
            break;
          }
          used[w] = true;
          cnt++;
          v = w;
        }
        if (ok) {
          S.push_back(cnt);
        }
      }
    }
    int cnt = S.size();
    map<int, int> mp;
    for (int i = 0; i < cnt; i++) {
      mp[S[i]]++;
    }
    int cnt2 = 0;
    vector<int> a, b;
    for (auto P : mp) {
      a.push_back(P.first);
      b.push_back(P.second);
      cnt2++;
    }
    vector<long long> dp(m + 1, 0);
    dp[0] = 1;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < cnt2; j++) {
        if (i + a[j] <= m) {
          dp[i + a[j]] += dp[i] * b[j];
          dp[i + a[j]] %= MOD;
        }
      }
    }
    cout << dp[m] << endl;
  }
  