#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;
const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;
#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))
#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
using ll=long long;
const int mod=786433,NR=1<<18,g=10,gi=235930;
//const int mod=998244353,g=3,gi=332748118,NR=1<<21;
int powmod(int a,int b){
int res=1;
for(;b>0;b>>=1,a=1ll*a*a%mod)if(b&1)res=1ll*res*a%mod;
return res;
}
int tt,n=100000,m,a[100009],fac[100009],inv[100009],A[NR],B[NR],rev[NR],lim,bit;
void NTT(int *a,int type){
for(int i=0;i<lim;++i)if(i<rev[i])swap(a[i],a[rev[i]]);
for(int k=1;k<lim;k<<=1){
int wn=powmod(type==1?g:gi,(mod-1)/(k<<1));
for(int i=0;i<lim;i+=(k<<1)){
int w=1;
for(int j=0;j<k;++j,w=1ll*w*wn%mod){
int x=a[i+j],y=1ll*w*a[i+j+k]%mod;
a[i+j]=(x+y)%mod,a[i+j+k]=(x-y+mod)%mod;
}
}
}
if(type==1)return ;
int invn=powmod(lim,mod-2);
for(int i=0;i<lim;++i){
a[i]=1ll*a[i]*invn%mod;
}
}
void cdq(int l,int r){
//cerr<<l<<"::"<<r<<"\n";
if(l==r){
//cout<<l<<"::"<<fac[l]<<"__"<<a[l]<<"\n";
a[l]=(fac[l]-a[l]+mod)%mod;
return ;
}
int mid=(l+r)>>1;
cdq(l,mid);
lim=1,bit=0;
while(lim<=(r-l+1)*2)lim<<=1,bit+=1;
for(int i=0;i<lim;++i)rev[i]=(rev[i>>1]>>1)|((i&1)<<(bit-1));
for(int i=0;i<lim;++i)A[i]=B[i]=0;
for(int i=l;i<=mid;++i)A[i-l]=a[i];
for(int i=0;i<=r-l+1;++i)B[i]=fac[i];
NTT(A,1),NTT(B,1);
for(int i=0;i<lim;++i)A[i]=1ll*A[i]*B[i]%mod;
NTT(A,0);
for(int i=mid+1;i<=r;++i)a[i]=(a[i]+A[i-l])%mod;
cdq(mid+1,r);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
fac[0]=inv[0]=1;
for(int i=1;i<=n;++i)fac[i]=1ll*fac[i-1]*i%mod;
inv[n]=powmod(fac[n],mod-2);
for(int i=n-1;i;--i)inv[i]=1ll*inv[i+1]*(i+1)%mod;
a[0]=1;
cdq(0,n);
cin>>tt;
f(sb,1,tt){
ll ans=1;
cin>>n>>m;
for(int i=1,x,mi,ma;i<=m;++i){
cin>>x;
mi=0x3f3f3f,ma=-1;
for(int j=1,y;j<=x;++j){
cin>>y;
mi=min(mi,y),ma=max(ma,y);
}
if(ma-mi+1!=x)ans=0;
else ans=1ll*ans*a[ma-mi+1]%mod;
}
cout<<ans<<"\n";
}
return 0;
}