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;
  template <typename Arg1>
  void __f(const char* name, Arg1&& arg1) {
    std::cerr << name << " : " << arg1 << endl;
  }
  template <typename Arg1, typename... Args>
  void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
    __f(comma + 1, args...);
  }
  const long long N = 3e5 + 9, mod = 998244353;
  vector<long long> adj[N];
  vector<long long> act_adj[N];
  bool vis[N];
  long long mark[N];
  long long dfs(long long v) {
    long long ans = 1;
    vis[v] = 1;
    long long flag = 0;
    if (mark[v] == 2) {
      flag = 1;
    }
    long long pika = 0;
    for (long long x : adj[v]) {
      if (vis[x]) {
        continue;
      }
      pika = 1;
      long long cnt = dfs(x);
      if (cnt == 0) flag = 1;
      ans += cnt;
    }
    if (pika == 0) {
      if (adj[v].size() > 1) {
        return 0;
      }
    }
    if (flag)
      return 0;
    else
      return ans;
  }
  void solve() {
    long long n, m, k;
    cin >> n >> m >> k;
    long long rem[k];
    memset(rem, -1, sizeof(rem));
    vector<long long> v[n];
    for (long long i = 0; i < n; i++) {
      long long c;
      cin >> c;
      vector<long long> tmp;
      for (long long j = 0; j < c; j++) {
        long long x;
        cin >> x;
        tmp.push_back(x);
        rem[x - 1] = 0;
      }
      v[i] = tmp;
    }
    for (long long i = 0; i < n; i++) {
      for (long long j = 1; j < v[i].size(); j++) {
        long long x = v[i][j - 1];
        long long y = v[i][j];
        if (y == v[i][0]) {
          mark[y] = 2;
        }
        if (mark[y] == 1 && !(act_adj[x].size() == 1 && act_adj[x][0] == y)) {
          mark[y] = 2;
        }
        if (x == y) {
          mark[x] = 2;
        }
        if (act_adj[x].size() == 1) {
          if (act_adj[x][0] != y) {
            mark[x] = 2;
          }
        } else {
          act_adj[x].push_back(y);
        }
        if ((x != y) &&
            (mark[y] == 0 ||
             !(mark[y] == 1 && (act_adj[x].size() == 1 && act_adj[x][0] == y)))) {
          adj[x].push_back(y);
          adj[y].push_back(x);
        }
        if (mark[y] == 0) mark[y] = 1;
      }
    }
    vector<long long> p(m + 1, 0);
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < v[i].size(); j++) {
        if (!vis[v[i][j]]) {
          long long cnt = dfs(v[i][j]);
          if (cnt <= m) {
            p[cnt]++;
          }
        }
      }
    }
    p[0] = 0;
    p[1] += (abs(accumulate(rem, rem + k, 0)));
    long long idx = -1;
    for (long long i = m; i >= 0; i--) {
      if (p[i] >= 1) {
        idx = i;
        break;
      }
    }
    if (idx == -1) {
      cout << "0\n";
      return;
    }
    map<long long, long long> mp;
    for (long long i = 1; i <= m; i++) {
      if (p[i] > 0) mp[i] = p[i];
    }
    long long dp[m + 1];
    memset(dp, 0, sizeof dp);
    dp[0] = 1;
    for (long long i = 1; i <= m; i++) {
      for (auto x : mp) {
        long long idx = x.first;
        long long val = x.second;
        if (idx > i) {
          break;
        }
        dp[i] += val * dp[i - idx] % mod;
        dp[i] %= mod;
      }
    }
    cout << dp[m] % mod;
  }
  signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long T = 1;
    while (T--) {
      solve();
    }
  }
  