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;
  mt19937 ran(time(0));
  long long mod = 998244353;
  long long gcd(long long a, long long b) {
    if (!b) return a;
    return gcd(b, a % b);
  }
  long long Bpow(long long a, long long b) {
    long long ans = 1;
    while (b) {
      if (b & 1) {
        ans *= a;
        ans %= mod;
      }
      a = a * a % mod;
      b >>= 1;
    }
    return ans;
  }
  bool F(vector<long long>& a, vector<long long>& b, long long c = 0) {
    bool f = true;
    for (long long i = 0; i < b.size() && c + i < a.size(); i++) {
      f &= (a[c + i] == b[i]);
    }
    return f;
  }
  vector<long long> g(3e5 + 7, -1);
  vector<long long> w(3e5 + 7);
  vector<long long> len(3e5 + 7);
  vector<long long> z(3e5 + 7);
  vector<long long> b(3e5 + 7);
  vector<long long> c;
  vector<vector<long long>> a(3e5 + 7);
  vector<long long> mn(3e5 + 7);
  long long dfs(long long v, long long now) {
    if (z[v]) return -1;
    if (g[v] == -1) return now;
    return dfs(g[v], now + w[v]);
  }
  long long pl(long long a, long long b) {
    long long ans = a + b;
    if (ans >= mod) ans -= mod;
    return ans;
  }
  int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long n, m, k;
    cin >> n >> m >> k;
    vector<long long> h(k + 1);
    vector<vector<long long>> fir(k + 1);
    bool f = false;
    set<long long> st;
    vector<vector<pair<long long, long long>>> ow(k + 1);
    for (long long i = 0; i < n; i++) {
      long long c;
      cin >> c;
      a[i].resize(c);
      for (long long j = 0; j < c; j++) {
        cin >> a[i][j];
        h[a[i][j]]++;
        if (h[a[i][j]] > 1) {
          z[i]++;
        }
        st.insert(a[i][j]);
        ow[a[i][j]].push_back({i, j});
      }
      fir[a[i][0]].push_back(i);
      for (long long j = 0; j < c; j++) {
        h[a[i][j]]--;
      }
    }
    long long kol = k - st.size();
    for (long long i = 1; i < k + 1; i++) {
      bool fs = true;
      if (ow[i].empty()) {
        continue;
      }
      set<long long> stl, str;
      for (auto& u : ow[i]) {
        if (u.second) stl.insert(a[u.first][u.second - 1]);
        if (u.second + 1 < a[u.first].size())
          str.insert(a[u.first][u.second + 1]);
      }
      if (stl.size() > 1 || str.size() > 1) {
        for (auto& u : ow[i]) {
          z[u.first]++;
        }
      }
    }
    vector<long long> fi(k + 1, -1);
    for (long long i = 1; i < k + 1; i++) {
      vector<long long> v;
      long long ind;
      for (long long j = 0; j < fir[i].size(); j++) {
        if (v.size() < a[fir[i][j]].size()) {
          v = a[fir[i][j]];
          ind = fir[i][j];
        }
      }
      for (long long j = 0; j < fir[i].size(); j++) {
        if (!F(v, a[fir[i][j]])) {
          z[ind]++;
        }
        if (fir[i][j] != ind) {
          b[fir[i][j]]++;
        }
      }
      if (v.empty()) continue;
      fi[i] = ind;
    }
    for (long long i = 0; i < n; i++) {
      if (z[i]) {
        for (long long j = 1; j < a[i].size(); j++) {
          if (fi[a[i][j]] != -1) {
            b[fi[a[i][j]]]++;
            z[fi[a[i][j]]]++;
          }
        }
      }
      for (long long j = 1; j < a[i].size(); j++) {
        if (fi[a[i][j]] != -1) {
          if (!F(a[i], a[fi[a[i][j]]], j)) {
            z[i]++;
          }
          b[fi[a[i][j]]]++;
          if (a[fi[a[i][j]]].size() > a[i].size() - j) {
            g[i] = fi[a[i][j]];
            w[i] = a[fi[a[i][j]]].size() - (a[i].size() - j);
            break;
          }
          b[fi[a[i][j]]]++;
        }
      }
    }
    vector<long long> um(3e5 + 7);
    vector<long long> v;
    for (long long i = 0; i < n; i++) {
      if (!b[i] && !z[i]) {
        long long val = dfs(i, a[i].size());
        c.clear();
        if (val == -1) continue;
        um[val]++;
        if (um[val] == 1) {
          v.push_back(val);
        }
      }
    }
    vector<long long> dp(m + 1);
    dp[0] = 1;
    for (long long i = 1; i < m + 1; i++) {
      dp[i] = dp[i - 1] * kol % mod;
      for (auto& u : v) {
        if (i - u < 0) continue;
        dp[i] = pl(dp[i], dp[i - u] * um[u] % mod);
      }
    }
    cout << dp[m];
  }
  