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;
  const int MAXN = 6E5;
  const int BLOCK = 550;
  int par[MAXN];
  int sz[MAXN];
  vector<int> a[MAXN];
  vector<int> who_[MAXN];
  bool isBad[MAXN];
  vector<int> objects;
  int dp[MAXN + 1];
  int freq[BLOCK];
  vector<int> adjList[MAXN];
  int inDeg[MAXN];
  int outDeg[MAXN];
  int getRoot(int x) {
    if (par[x] != x) par[x] = getRoot(par[x]);
    return par[x];
  }
  bool mge(int x, int y) {
    x = getRoot(x);
    y = getRoot(y);
    if (x == y) return false;
    if (sz[x] < sz[y]) swap(x, y);
    par[y] = x;
    sz[x] += sz[y];
    for (int z : who_[y]) who_[x].push_back(z);
    return true;
  }
  void add(int &x, int y) {
    x += y;
    if (x >= MOD) x -= MOD;
  }
  int solve(int n) {
    vector<int> big;
    for (int num : objects) {
      if (num >= BLOCK)
        big.push_back(num);
      else
        freq[num]++;
    }
    dp[0] = 1;
    for (int i = 1; i <= n; i++) {
      for (int b : big)
        if (i - b >= 0) add(dp[i], dp[i - b]);
      for (int j = 1; j < BLOCK; j++)
        if (i - j >= 0) add(dp[i], (int)((long long)freq[j] * dp[i - j] % MOD));
    }
    return dp[n];
  }
  int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    for (int i = 0; i < MAXN; i++) {
      par[i] = i;
      sz[i] = 1;
    }
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++) {
      int c;
      cin >> c;
      a[i] = vector<int>(c);
      set<int> seen;
      for (int j = 0; j < c; j++) {
        cin >> a[i][j];
        a[i][j]--;
        if (seen.count(a[i][j])) isBad[i] = true;
        seen.insert(a[i][j]);
        if (j) {
          adjList[a[i][j - 1]].push_back(a[i][j]);
          outDeg[a[i][j - 1]]++;
          inDeg[a[i][j]]++;
        }
      }
    }
    for (int i = 0; i < k; i++) {
      a[n + i] = {i};
    }
    n += k;
    for (int i = 0; i < n; i++) {
      int num = a[i][0];
      who_[getRoot(num)].push_back(i);
      for (int j : a[i]) {
        mge(j, num);
      }
    }
    for (int i = 0; i < k; i++) {
      if (getRoot(i) != i) continue;
      int r = getRoot(i);
      bool valid = true;
      set<int> nums;
      for (int ind : who_[r]) {
        if (isBad[ind]) valid = false;
        for (int x : a[ind]) nums.insert(x);
      }
      if (!valid) continue;
      int sources = 0;
      int sinks = 0;
      for (int x : nums) {
        if (!inDeg[x]) sources++;
        if (!outDeg[x]) sinks++;
        if (outDeg[x]) {
          for (int y : adjList[x])
            if (y != adjList[x].back()) valid = false;
        }
      }
      if (sources != 1 || sinks != 1) valid = false;
      if (!valid) continue;
      objects.push_back(sz[r]);
    }
    int ans = solve(m);
    cout << ans << "\n";
    return 0;
  }
  