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 N = 3 * 1e5 + 100;
  long long n, m, k, vi[N], l[N], r[N], rev[N << 2], len;
  long long dp[N], g[N], pr[N];
  long long w, b[N], c[N], Inv[N];
  pair<long long, long long> t[N];
  vector<long long> A[N], p[N], f[25];
  inline void add(long long &a, long long b) {
    a = ((a + b >= 998244353) ? a + b - 998244353 : a + b);
  }
  inline void del(long long &a, long long b) {
    a = ((a - b < 0) ? a - b + 998244353 : a - b);
  }
  inline void mul(long long &a, long long b) { a = a * b % 998244353; }
  inline long long m_pow(long long a, long long b) {
    long long ans = 1;
    while (b) {
      if (b & 1) mul(ans, a);
      b >>= 1;
      mul(a, a);
    }
    return ans;
  }
  inline long long read() {
    long long f = 1, x = 0;
    char s = getchar();
    while (s < '0' || s > '9') {
      if (s == '-') f = -1;
      s = getchar();
    }
    while (s >= '0' && s <= '9') {
      x = x * 10 + s - '0';
      s = getchar();
    }
    return x * f;
  }
  void prework() {
    long long d = 20;
    for (long long i = 1; i <= d; i++) {
      f[i].resize(1 << i);
      f[i][0] = 1;
      f[i][1] = m_pow(3, (998244353 - 1) >> i);
      for (long long j = 2; j < (1 << i); j++)
        f[i][j] = f[i][j - 1] * f[i][1] % 998244353;
    }
  }
  inline void change(long long len) {
    for (long long i = 0; i < len; i++) {
      rev[i] = rev[i >> 1] >> 1;
      if (i & 1) rev[i] |= (len >> 1);
    }
  }
  inline void DFT(vector<long long> &y, long long len, long long v) {
    for (long long i = 0; i < len; i++)
      if (i < rev[i]) swap(y[i], y[rev[i]]);
    for (long long i = 1, d = 1; i < len; i <<= 1, d++) {
      for (long long j = 0; j < len; j += (i << 1)) {
        long long *W = &f[d][0], a, b;
        for (long long k = j; k < j + i; k++) {
          a = y[k], b = y[k + i] * (*W++) % 998244353;
          y[k] = y[k + i] = a;
          add(y[k], b);
          del(y[k + i], b);
        }
      }
    }
    if (v == -1) {
      reverse(y.begin() + 1, y.end());
      long long invlen = m_pow(len, 998244353 - 2);
      for (long long i = 0; i < len; i++) mul(y[i], invlen);
    }
  }
  vector<long long> operator*(vector<long long> a, vector<long long> b) {
    if (a.empty()) return b;
    if (b.empty()) return a;
    long long n = (long long)a.size(), m = (long long)b.size();
    len = 1;
    while (len < n + m) len <<= 1;
    change(len);
    a.resize(len);
    b.resize(len);
    DFT(a, len, 1);
    DFT(b, len, 1);
    for (long long i = 0; i < len; i++) mul(a[i], b[i]);
    DFT(a, len, -1);
    a.resize(n + m - 1);
    return a;
  }
  void solve(long long l, long long r) {
    if (l == r) {
      if (l == 0) dp[l] = 1;
      return;
    }
    long long mid = (l + r) >> 1;
    solve(l, mid);
    vector<long long> A;
    A = vector<long long>(dp + l, dp + mid + 1) *
        vector<long long>(g, g + r - l + 1);
    for (long long i = mid + 1; i <= r; i++) add(dp[i], A[i - l]);
    solve(mid + 1, r);
  }
  void del(long long id) {
    vi[id] = 1;
    for (long long i : A[id])
      if (t[i].first != -1) {
        vi[t[i].first] = 1;
        t[i] = make_pair(-1, -1);
      }
  }
  void check_twice(long long id) {
    static long long cnt[N];
    for (long long i : A[id]) cnt[i]++;
    bool bl = 1;
    for (long long i : A[id])
      if (cnt[i] > 1) bl = 0;
    if (!bl) del(id);
    for (long long i : A[id]) cnt[i] = 0;
  }
  void upd(long long x) {
    if (t[x].first <= 0) return;
    if (vi[t[x].first]) t[x] = make_pair(-1, -1);
  }
  void merge(long long &x, long long &y) {
    if ((long long)p[x].size() < (long long)p[y].size()) {
      l[y] = l[y] - (long long)p[x].size();
      for (long long i : p[x]) t[i] = make_pair(y, t[i].second - l[x] + l[y]);
      p[y].insert(p[y].end(), p[x].begin(), p[x].end());
      vi[x] = 1;
      x = y;
    } else {
      r[x] = r[x] + (long long)p[y].size();
      for (long long i : p[y]) t[i] = make_pair(x, r[x] - (r[y] - t[i].second));
      p[x].insert(p[x].begin(), p[y].begin(), p[y].end());
      vi[y] = 1;
      y = x;
    }
  }
  bool cmp(long long a, long long b) { return t[a].second < t[b].second; }
  bool cmp1(long long a, long long b) {
    return (long long)A[a].size() > (long long)A[b].size();
  }
  signed main() {
    prework();
    n = read();
    m = read();
    k = read();
    for (long long i = 1; i <= n; i++) {
      long long c = read();
      A[i].resize(c);
      for (long long &j : A[i]) j = read();
      check_twice(i);
    }
    for (long long i = 1; i <= n; i++) pr[i] = i;
    sort(pr + 1, pr + 1 + n, cmp1);
    for (long long s = 1; s <= n; s++) {
      long long i = pr[s];
      if (vi[i]) continue;
      bool bl = 1;
      for (long long j : A[i]) {
        upd(j);
        if (t[j].first == -1) bl = 0;
      }
      if (!bl) {
        del(i);
        continue;
      }
      w = 0;
      for (long long j = 0; j < (long long)A[i].size();) {
        long long k = j;
        while (k < (long long)A[i].size() && t[A[i][j]].first == t[A[i][k]].first)
          k++;
        b[++w] = k - j;
        c[w] = t[A[i][j]].first;
        j = k;
      }
      bl = 1;
      for (long long j = 1, s = 0; j <= w; s += b[j++])
        if (c[j] != 0) {
          for (long long k = s; k < s + b[j] - 1; k++)
            if (t[A[i][k]].second + 1 != t[A[i][k + 1]].second) bl = 0;
          long long x = A[i][s], y = A[i][s + b[j] - 1];
          if (t[x].second != l[c[j]] && t[y].second != r[c[j]] && w > 1) bl = 0;
          if (t[x].second == l[c[j]] && j != w) bl = 0;
          if (t[y].second == r[c[j]] && j != 1) bl = 0;
        }
      if (w > 3 || (w == 3 && c[2] != 0) || !bl) {
        del(i);
        continue;
      }
      for (long long j = 1, s = 0; j <= w; s += b[j++])
        if (c[j] == 0) {
          for (long long k = s; k < s + b[j]; k++)
            t[A[i][k]] = make_pair(i, r[i]++), p[i].push_back(A[i][k]);
          c[j] = i;
        }
      r[i]--;
      for (long long i = 1; i < w; i++) merge(c[i], c[i + 1]);
    }
    for (long long i = 1; i <= n; i++)
      if (!vi[i] && !p[i].empty()) {
        g[(long long)p[i].size()]++;
      }
    long long ret = 0;
    for (long long i = 1; i <= k; i++)
      if (t[i].first == 0) ret++;
    g[1] += ret;
    solve(0, m);
    printf("%lld\n", dp[m]);
  }
  