#include<bits/stdc++.h>
#define ll long long 
#define endl putchar('\n')
#define ygkg putchar(' ')
const ll inf=10000000000000000;
const ll mod=1000000007;
namespace io
{
	template<typename LX>
	void in(LX &ygs)
	{
		ygs=0;bool pdfh=0;char ch=getchar();
		while(!isdigit(ch)) pdfh=ch=='-',ch=getchar();
		do{ygs=(ygs<<3)+(ygs<<1)+(ch^48),ch=getchar();}while(isdigit(ch));
		if(pdfh) ygs=-ygs;
	}
	template<typename LX>
	void _out(LX ygs){if(ygs) _out(ygs/10),putchar((ygs%10)^48);return;}
	template<typename LX>
	void out(LX ygs){_out((ygs>0)?(ygs):(ygs?(putchar('-'),-ygs):(putchar('0'),0)));return;}
}
using namespace io;
using namespace std;
ll hd[100015],le;
struct ee{ll to,nx;}e[200015];
void lj(ll uu,ll vv){e[++le]=(ee){vv,hd[uu]},hd[uu]=le;}
ll c,T;
ll n,k,a[100015],b[100015],ans,sum,ed[100015];
ll dfs(ll uu,ll fa)
{
	ll f=1,ff=1,fff=0;
	for(ll i=hd[uu];i;i=e[i].nx)
	{
		ll vv=e[i].to;
		if(vv==fa) continue;
		f=(f*dfs(vv,uu))%mod;
		++fff,ff=(ff*fff)%mod;
	}
	return (f*ff)%mod;
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	in(c),in(T);
	while(T--)
	{
		in(n),in(k);
		ans=sum=1;
		le=0;
		for(ll i=1;i<=n;++i) hd[i]=0;
		for(ll i=1;i<n;++i)
			in(a[i]),in(b[i]),lj(a[i],b[i]),lj(b[i],a[i]);
		for(ll i=1;i<=k;++i) in(ed[i]);
		if(c==18) out(1),endl;
		else if(c>=19&&c<=21)
		{
			for(ll i=1;i<n-1;++i) ans=(ans*i)%mod;
			for(ll i=1;i<n-2;++i) sum=(sum*i)%mod;
			out(((ans*k)%mod-(sum*((k*(k-1)/2)%mod))%mod+mod)%mod),endl;
		}
		else
		{
			if(c<=6)
			{
				out((dfs(a[ed[1]],b[ed[1]])*dfs(b[ed[1]],a[ed[1]]))%mod),endl;
			}
			else
			{
				out((((dfs(a[ed[1]],b[ed[1]])*dfs(b[ed[1]],a[ed[1]]))%mod)*k)%mod),endl;
			}
		}
	}
	return 0;
}
