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;
  const int maxi = 3e5 + 100;
  int n, m, k;
  set<int> alfabet;
  vector<vector<int> > a;
  vector<int> g[maxi];
  set<int> liter[maxi];
  set<int> zbior;
  int odw[maxi];
  set<int> zak;
  map<int, int> dl;
  int odwslo[maxi];
  vector<pair<int, int> > eds;
  set<int> wych[maxi], wcho[maxi];
  map<pair<int, int>, bool> odweds;
  void dfszakslow(int w);
  void dfszakliter(int w) {
    if (zak.find(w) != zak.end()) {
      return;
    }
    zak.insert(w);
    for (auto xd : liter[w]) {
      if (!odwslo[xd]) {
        dfszakslow(xd);
      }
    }
  }
  void dfszakslow(int w) {
    if (odwslo[w]) {
      return;
    }
    odwslo[w] = 1;
    for (auto xd : a[w]) {
      dfszakliter(xd);
    }
  }
  bool dfs(int w) {
    zbior.insert(w);
    odw[w] = 1;
    bool ans = true;
    if (zak.find(w) != zak.end()) {
      ans = false;
    }
    for (auto xd : g[w]) {
      if (odweds[eds[xd]]) {
        continue;
      }
      odweds[eds[xd]] = true;
      int x = eds[xd].first;
      if (x == w) {
        x = eds[xd].second;
      }
      if (odw[x]) {
        ans = false;
      }
      bool dx = dfs(x);
      ans &= dx;
    }
    return ans;
  }
  int main() {
    scanf("%d%d%d", &n, &m, &k);
    vector<int> zleslowa;
    for (int i = 0; i < n; ++i) {
      int c;
      scanf("%d", &c);
      vector<int> v;
      set<int> sv;
      multiset<int> mv;
      for (int j = 0; j < c; ++j) {
        int x;
        scanf("%d", &x);
        alfabet.insert(x);
        v.push_back(x);
        liter[x].insert(i);
        sv.insert(x);
        mv.insert(x);
      }
      a.push_back(v);
      bool flf = false;
      for (int j = 1; j < c; ++j) {
        eds.push_back({v[j - 1], v[j]});
        g[v[j]].push_back(eds.size() - 1);
        g[v[j - 1]].push_back(eds.size() - 1);
        wcho[v[j]].insert(v[j - 1]);
        wych[v[j - 1]].insert(v[j]);
        if (wcho[v[j]].size() > 1 || wych[v[j - 1]].size() > 1) {
          flf = true;
        }
      }
      if (sv.size() != mv.size() || flf) {
        zleslowa.push_back(i);
      }
    }
    for (auto xd : zleslowa) {
      dfszakslow(xd);
    }
    for (int i = 1; i <= k; ++i) {
      if (odw[i]) {
        continue;
      }
      zbior.clear();
      bool fla = dfs(i);
      if (fla) {
        ++dl[zbior.size()];
      }
    }
    vector<long long> dp(m + 1, 0);
    dp[0] = 1;
    for (int i = 1; i <= m; ++i) {
      for (auto xd : dl) {
        if (xd.first > i) {
          break;
        }
        dp[i] += dp[i - xd.first] * xd.second;
        dp[i] %= mod;
      }
    }
    printf("%lld\n", dp[m]);
  }
  