#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
const long long mod=1e9+7;
const int MAXN=1e5+10;
int id,T;
int n,k;
int u,v;
map<int,pii>side;
int e[MAXN];
int d[MAXN];
long long ans=1;
void f(int tp)
{
	if(tp==1)
	{
		memset(d,0,sizeof(d));
		for(int i=1;i<n;i++)
		{
			scanf("%d%d",&u,&v);
			d[u]++;
			d[v]++;
		}
		for(int i=1;i<=k;i++)scanf("%d",&e[i]);
		for(int i=1;i<=n;i++)if(d[i]==3)ans=(ans*2)%mod;
		printf("%lld\n",ans);
	}
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%d%d",&id,&T);
	while(T--)
	{
		scanf("%d%d",&n,&k);
		if(id<=6)
		{
			f(1);
		}
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
 } 
