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>
  #pragma GCC optimize("Ofast")
  #pragma GCC optimize("unroll-loops")
  #pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native")
  using namespace std;
  long long gcd(long long i, long long j) {
    if (j == 0)
      return i;
    else
      return gcd(j, i % j);
  }
  template <typename T>
  inline T getint() {
    T val = 0;
    char c;
    bool neg = false;
    while ((c = getchar()) && !(c >= '0' && c <= '9')) {
      neg |= c == '-';
    }
    do {
      val = (val * 10) + c - '0';
    } while ((c = getchar()) && (c >= '0' && c <= '9'));
    return val * (neg ? -1 : 1);
  }
  const long long INF = 3e18 + 100;
  const int mod = 998244353;
  const double eps = 1e-11, pi = acos(-1);
  const long long maxN = 1 << 20, maxT = 600100, A = 179, K = 170;
  mt19937 mt_rand(time(0));
  long long bp(long long et, long long b) {
    b %= mod - 1;
    et %= mod;
    long long res = 1;
    for (int i = 30; i >= 0; --i) {
      res = (res * res) % mod;
      if ((b & (1 << i)) != 0) res = (res * et) % mod;
    }
    return res;
  }
  void panic() {
    cout << "-1\n";
    exit(0);
  }
  vector<vector<int>> g;
  vector<int> used;
  vector<set<int>> to(maxN);
  int dfs(int v) {
    used[v] = 1;
    sort(g[v].begin(), g[v].end());
    g[v].resize(unique(g[v].begin(), g[v].end()) - g[v].begin());
    if (g[v].size() > 1) return -1;
    if (to[v].size() > 1) return -1;
    int l = 1;
    if (g[v].empty()) return l;
    if (used[g[v][0]]) return -1;
    int dd = dfs(g[v][0]);
    if (dd == -1) return -1;
    return l + dd;
  }
  void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    g.assign(k, vector<int>());
    vector<int> d(k), bd(k);
    vector<vector<int>> tmp(n);
    for (int i = 0; i < n; ++i) {
      int c;
      cin >> c;
      tmp[i].resize(c);
      set<int> vert;
      for (auto& x : tmp[i]) {
        cin >> x;
        ++d[--x];
      }
      for (int j = 0; j + 1 < tmp[i].size(); ++j) {
        g[tmp[i][j]].push_back(tmp[i][j + 1]);
        to[tmp[i][j + 1]].insert(tmp[i][j]);
      }
    }
    used.assign(k, 0);
    vector<int> lns(k + 1);
    for (int i = 0; i < k; ++i) {
      if (d[i] == 0) {
        ++lns[1];
        continue;
      }
      if (!to[i].empty() || used[i]) continue;
      int tmp = dfs(i);
      if (tmp == -1) continue;
      ++lns[tmp];
    }
    vector<long long> dp(m + 1, 0);
    dp[0] = 1;
    set<int> s;
    for (int i = 0; i <= k; ++i) {
      if (lns[i] != 0) s.insert(i);
    }
    for (int i = 0; i < m; ++i) {
      for (auto x : s) {
        if (i + x > m) break;
        dp[i + x] = (dp[i + x] + dp[i] * lns[x]) % mod;
      }
    }
    cout << dp.back() << "\n";
  }
  int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout.precision(20);
    srand(time(0));
    int t = 1;
    while (t--) {
      solve();
    }
    return 0;
  }
  