#include<bits/stdc++.h>
using namespace std;
namespace IO_fast{
	char bufin[1<<20],*fs,*ft;
	char getc(){
		return (fs==ft&&(ft=(fs=bufin)+fread(bufin,1,1<<20,stdin),fs==ft)?EOF:*fs++);
	} 
	int read(){
		int x=0;
		char ch=getc();
		while(ch<'0'||ch>'9') ch=getc();
		while(ch>='0'&&ch<='9'){
			x=x*10+(ch^'0');
			ch=getc();
		}
		return x;
	}
}
using namespace IO_fast;
const int N=100005,mod=1000000007;
struct Map{
    struct edge{
        int to,nxt;
    }e[N<<1];
    int st[N],num;
    void clear(){
        memset(st,0,sizeof(st));
        num=0;
    }
    void adde(int x,int y){
        e[++num]=(edge){y,st[x]},st[x]=num;
    }
}ori,aft;
int deg[N];
int rec[N][2];
int fac[N];
int pw[N];
int dfs(int u,int pre){
    if(u==0) return 1;
    int res=fac[deg[u]-1],cnt=0;
    for(int i=aft.st[u];i;i=aft.e[i].nxt){
        int v=aft.e[i].to;
        if(v==pre) continue;
        res=1ll*res*dfs(v,u)%mod;
        ++cnt;
    }
    return 1ll*res*(cnt>1?pw[cnt-1]:1)%mod;
}
void solve1(){
    int n,m;
    cin>>n>>m;
    ori.clear();
    aft.clear();
    memset(deg,0,sizeof(deg));
    fac[0]=1;
    for(int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod;
    pw[0]=1;
    for(int i=1;i<=n;i++) pw[i]=(pw[i-1]<<1)%mod;
    for(int i=1,x,y;i<n;i++){
        scanf("%d%d",&x,&y);
        ori.adde(x,y);
        ori.adde(y,x);
        deg[x]++,deg[y]++;
        rec[i][0]=x,rec[i][1]=y;
    }
    for(int u=1;u<=n;u++){
        if(deg[u]<=1) continue;
        for(int i=ori.st[u];i;i=ori.e[i].nxt){
            int v=ori.e[i].to;
            if(deg[v]>1){
                aft.adde(u,v);
            }
        }
    }
    int rt;
    cin>>rt;
    int x=0,y=0;
    if(deg[rec[rt][0]]>1) x=rec[rt][0];
    if(deg[rec[rt][1]]>1) y=rec[rt][1];
    cout<<1ll*dfs(x,0)*dfs(y,0)%mod<<endl;
}
void solve18(){
    int n=read(),m=read();
    for(int i=1,x,y;i<n;i++)
        x=read(),y=read();
    for(int i=1,x;i<=m;i++) x=read();
    cout<<1<<endl;
}
void solve19_21(){
    int n=read(),m=read();
    for(int i=1,x,y;i<n;i++)
        x=read(),y=read();
    for(int i=1,x;i<=m;i++) x=read();
    if(n==2){
    	cout<<1<<endl;
    	return;
	}
    --n;
    int res=(0ll+1ll*m*(n-m)%mod+1ll*m*(m-1)/2%mod)%mod;
    for(int i=1;i<=n-2;i++) res=1ll*res*i%mod;
    cout<<res<<endl;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
    int tp,t;
    cin>>tp>>t;
    if(tp==18){
    	for(;t;--t) solve18();
    	return 0;
	}
	if(tp>=19&&tp<=21){
		for(;t;--t) solve19_21();
		return 0;
	}
    if(tp<=6){
        for(;t;--t) solve1();
        return 0;
    }
}
