constint N = 110; int A[N][N]; int degin[N], degout[N]; int T, n; int fac[300000], inv[300000], ifac[300000];
intqpow(int a, int b){ int res = 1; while (b) { if (b & 1) res = 1ll * res * a % mod; a = 1ll * a * a % mod; b >>= 1; } return res; }
intadd(int a, int b){ return a + b >= mod ? a + b - mod : a + b; }
intsub(int a, int b){ return a - b < 0 ? a - b + mod : a - b; }
intdet(int n){ int res = 1; for (int i = 2; i <= n; ++i) { if (!degout[i]) continue; if (!A[i][i]) { for (int j = i + 1; j <= n; ++j) { if (A[j][i]) { swap(A[i], A[j]); res = -res; break; } } } for (int j = i + 1; j <= n; ++j) { int div = 1ll * A[j][i] * qpow(A[i][i], mod - 2) % mod; for (int k = i; k <= n; ++k) { A[j][k] = sub(A[j][k], 1ll * div * A[i][k] % mod); } } } res = (mod + res) % mod; for (int i = 2; i <= n; ++i) { res = 1ll * res * A[i][i] % mod; } return res; }
signedmain(){ scanf("%d", &T); fac[0] = fac[1] = ifac[0] = ifac[1] = inv[1] = 1; for (int i = 2; i <= 220000; ++i) { fac[i] = 1ll * fac[i - 1] * i % mod; inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; ifac[i] = 1ll * ifac[i - 1] * inv[i] % mod; } while (T--) { scanf("%d", &n); memset(A, 0, sizeof(A)); memset(degin, 0, sizeof(degin)); memset(degout, 0, sizeof(degout)); for (int i = 1; i <= n; ++i) { int s; scanf("%d", &s); for (int j = 1; j <= s; ++j) { int x; scanf("%d", &x); A[i][x] = sub(A[i][x], 1); ++degin[x]; ++degout[i]; } A[i][i] = add(A[i][i], s); } if (n == 1) { puts("1"); continue; } int flag = 0; for (int i = 1; i <= n; ++i) { if (degin[i] != degout[i]) { flag = 1; } } if (flag == 1) { puts("0"); continue; } int res = det(n); res = 1ll * res * fac[degout[1]] % mod; for (int i = 2; i <= n; ++i) { res = 1ll * res * fac[degout[i] - 1] % mod; } printf("%d\n", res); } return0; }