#include #include #include #include #define mo 1000000007 using namespace std; struct edge{int go,next;} e[400005]; vector v[200005][17]; vector::iterator it; int n,p[200005],pri[200005],pn,cnt,ljb[200005],vv[200005][17], a[200005],is[200005],f[200005],size[200005],col[200005],maxmi[200005]; long long mi[200],ans,anss; int gf(int x){ if (f[x]==x) return x; f[x]=gf(f[x]); return f[x]; } void merge(int x,int y){ int fx=gf(x),fy=gf(y); size[fy]+=size[fx]; f[fx]=fy; } long long ksm(long long a,long long b){ long long ret=1ll; for (long long lj=a;b;b/=2,lj=lj*lj%mo) if (b&1) ret=ret*lj%mo; return ret; } void read(int &a){ char c;bool f=0;a=0; while(!((((c=getchar())>='0')&&(c<='9'))||(c=='-'))); if(c!='-')a=c-'0';else f=1; while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0'; if(f)a=-a; } int main(){ for (int i=2;i<=100000;i++){ if (!p[i]) p[i]=pri[++pn]=i; for (int j=1;i*pri[j]<=100000&&j<=pn;j++){ p[i*pri[j]]=pri[j]; if (i%pri[j]==0) break; } } for (int i=1;i<=pn;i++){ int td=1; while (td<=100000) td*=pri[i],maxmi[i]++; } int tt;read(tt); for (;tt--;){ read(n);cnt=0;for (int i=0;i<=n;i++) ljb[i]=0; for (int i=1;i1){ int nowp=p[td],k=0; while (p[td]==nowp) td/=nowp,k++; if (vv[nowp][k]!=tt){vv[nowp][k]=tt;v[nowp][k].clear();} v[nowp][k].push_back(i); } }mi[0]=1; ans=1;for (int i=0;i<=n;i++) col[i]=0; for (int i=1;i<=pn;i++){ for (int j=maxmi[i];j;j--) if (vv[pri[i]][j]==tt){ for (it=v[pri[i]][j].begin();it!=v[pri[i]][j].end();it++){ int id=*it; f[id]=id;size[id]=1; } } for (int j=1;j<=maxmi[i];j++) mi[j]=mi[j-1]*pri[i]%mo; for (int j=maxmi[i];j;j--) if (vv[pri[i]][j]==tt){ long long s=0; for (it=v[pri[i]][j].begin();it!=v[pri[i]][j].end();it++){ int id=*it,td=0;col[id]=i; for (int k=ljb[id];k;k=e[k].next){ if (col[e[k].go]==i){ s+=1ll*td*size[gf(e[k].go)]; td+=size[gf(e[k].go)]; } } for (int k=ljb[id];k;k=e[k].next) if (col[e[k].go]==i) merge(e[k].go,id); s+=size[gf(id)]; } ans=ans*ksm(mi[j],s)%mo; } } int m;read(m);for (int i=0;i<=n;i++) is[i]=0; for (int i=1;i<=m;i++){int x;read(x);is[x]=1;} anss=1;for (int i=0;i<=n;i++) col[i]=0; for (int i=1;i<=pn;i++){ for (int j=maxmi[i];j;j--) if (vv[pri[i]][j]==tt){ for (it=v[pri[i]][j].begin();it!=v[pri[i]][j].end();it++){ int id=*it; if (is[id]) continue; f[id]=id;size[id]=1; } } for (int j=1;j<=maxmi[i];j++) mi[j]=mi[j-1]*pri[i]%mo; for (int j=maxmi[i];j;j--) if (vv[pri[i]][j]==tt){ long long s=0; for (it=v[pri[i]][j].begin();it!=v[pri[i]][j].end();it++){ int id=*it,td=0; if (is[id]) continue; col[id]=i; for (int k=ljb[id];k;k=e[k].next){ if (col[e[k].go]==i){ s+=1ll*td*size[gf(e[k].go)]; td+=size[gf(e[k].go)]; } } for (int k=ljb[id];k;k=e[k].next) if (col[e[k].go]==i) merge(e[k].go,id); s+=size[gf(id)]; } anss=anss*ksm(mi[j],s)%mo; } } printf("%d\n",ans*ksm(anss,mo-2)%mo); } }