#include<iostream>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<climits>
//#define NDEBUG
#include<cassert>
#include<complex>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
//#define LL __int128
#define LL long long
#define ULL unsigned LL
#define uint unsigned int
//#define int LL
//#define double long double
#define par pair<int,int>
#define mkp make_pair
#define epb emplace_back
#define psb push_back
#define f(x) ((x).first)
#define s(x) ((x).second)
using namespace std;
#define Lbt(x) ((x)&(-(x)))
#define Swap(x,y) (x^=y^=x^=y)
const int Mxxx=1e5;
inline char gc()
{
static char buf[Mxxx],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,Mxxx,stdin),p1==p2)?EOF:*p1++;
}
inline char pc(char ch,bool fl=false)
{
static char buf[Mxxx],*p1=buf,*p2=buf+Mxxx;
return (fl||((*p1++=ch)&&p1==p2))&&(fwrite(buf,1,p1-buf,stdout),p1=buf),0;
}
//#define gc getchar
//#define pc(x,y) y?0:putchar(x)
#define output pc('!',true)
inline int read()
{
char ch=gc();
int gflag=0,gans=0;
while(ch<'0'||ch>'9')
{
gflag|=(ch=='-');
ch=gc();
}
while('0'<=ch&&ch<='9')
{
gans=(gans<<1)+(gans<<3)+(ch^48);
ch=gc();
}
return gflag?-gans:gans;
}
template<typename T>
inline char read(T &gans)
{
char ch=gc();
int gflag=0;
gans=0;
while(ch<'0'||ch>'9')
{
gflag|=(ch=='-');
ch=gc();
}
while('0'<=ch&&ch<='9')
{
gans=(gans<<1)+(gans<<3)+(ch^48);
ch=gc();
}
gans=gflag?-gans:gans;
return ch;
}
template<typename T>
inline void write(T x)
{
if(x>9)
{
write(x/10);
}
pc(x%10^48);
}
template<typename T>
inline void writenum(T x,char ch)
{
if(x<0)
{
pc('-');
x=-x;
}
write(x);
pc(ch);
}
inline void writechar(char x,char ch)
{
pc(x);
pc(ch);
}
template<typename T>
inline T Min(T x,T y)
{
return x<y?x:y;
}
template<typename T>
inline T Max(T x,T y)
{
return x>y?x:y;
}
template<typename T>
inline T Abs(T x)
{
return x>0?x:-x;
}
template<typename T>
inline void ckmx(T &x,T y)
{
x=Max(x,y);
}
template<typename T>
inline void ckmn(T &x,T y)
{
x=Min(x,y);
}
const int Mod=1e6+3;
#define Md(k) ((k)%Mod)
#define Ml(x,y) Md((LL)(x)*(y))
inline int Ad(int x,int y=0)
{
return (x+=y)>=Mod?x-Mod:x;
}
inline void Add(int &x,int y)
{
x=Ad(x,y);
}
inline void Mul(int &x,int y)
{
x=Ml(x,y);
}
inline int Ksm(int x,int y)
{
int t=1;
for(;y;y>>=1)
{
if(y&1)Mul(t,x);Mul(x,x);
}
return t;
}
inline int Inv(int x)
{
return Ksm(x,Mod-2);
}
const int Mx=100;
const int M=2e5;
int TT,n,fac[M+5],du[Mx],fa[Mx],sz[Mx],G[Mx][Mx];
inline int Gf(int x)
{
return fa[x]==x?x:fa[x]=Gf(fa[x]);
}
inline void Mrg(int x,int y)
{
int fx=Gf(x),fy=Gf(y);
if(fx^fy)
{
fa[fx]=fy;
sz[fy]+=sz[fx];
}
}
inline int Calc(int x)
{
int i,j,k,v,s=1,inv;
for(i=1;i<=x;i++)
{
if(!G[i][i])continue;Mul(s,G[i][i]);inv=Inv(G[i][i]);
for(j=i+1;j<=x;j++)if((v=G[j][i]))for(Mul(v,inv),k=i;k<=x;k++)Add(G[j][k],Mod-Ml(G[i][k],v));
}
return s;
}
int ans;
signed main()
{
#ifndef ONLINE_JUDGE
freopen("_.in","r",stdin);
// freopen("_.out","w",stdout);
#endif
int i,j,t,x;
for(fac[0]=i=1;i<=M;i++)fac[i]=Ml(fac[i-1],i);
for(TT=read();TT;TT--)
{
n=read();
for(i=0;i<n;i++)sz[fa[i]=i]=0;
for(ans=1,i=0;i<n;i++)
{
du[i]-=(G[i][i]=t=read());
if(t)Mul(ans,fac[t-(i!=0)]);
for(;t;t--)
{
du[x=read()-1]++;G[i][x]+=(--G[i][x])<0?Mod:0;
Mrg(i,x);sz[Gf(i)]++;
}
}
for(i=0;i<n;i++)if(du[i]||(Gf(i)^Gf(0)&&sz[Gf(i)]))ans=0;
writenum(ans?Ml(ans,Calc(n-1)):0,10);
for(i=0;i<n;i++)for(du[i]=j=0;j<n;G[i][j++]=0);
}
return output;
}
/*
2
1
0
2
1 1
1 2
*/
/*
5
2
1 2
1 1
3
2 2 3
1 1
1 1
4
3 2 3 4
1 1
1 1
1 1
5
4 2 3 4 5
1 1
1 1
1 1
1 1
6
5 2 3 4 5 6
1 1
1 1
1 1
1 1
1 1
*/
/*
1
3
1 2
1 1
0
*/
/*
1 3
2 2 3
1 1
1 3
*/
/*
1
3
2 2 3
2 1 3
2 1 2
*/剪贴板
剪贴板 9ehs6eul
剪贴板信息
剪贴板信息
- 收录
- 更新