/*
luogu P4084
*/
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9+7;
int n,m,root,fa[100005],f[100005][4],c[100005],w[4];
int ee=0,h[100005],nex[100005],to[100005];
void addedge(int x,int y)
{
nex[++ee] = h[x], to[ee] = y, h[x] = ee;
}
void dfs(int x)
{
for(int i=h[x];i;i=nex[i])
if(to[i]!=fa[x])
{
fa[to[i]] = x;
dfs(to[i]);
}
w[1] = w[2] = w[3] = 1;
for(int i=h[x];i;i=nex[i])
if(to[i]!=fa[x])
{
w[1] = w[1]*(f[to[i]][2]+f[to[i]][3])%M;
w[2] = w[2]*(f[to[i]][1]+f[to[i]][3])%M;
w[3] = w[3]*(f[to[i]][1]+f[to[i]][2])%M;
}
if(c[x]==0)
f[x][1] = w[1], f[x][2] = w[2], f[x][3] = w[3];
else
f[x][c[x]] = w[c[x]];
if(c[fa[x]]>0)
f[x][c[fa[x]]] = 0;
}
int main()
{
cin>>n>>m;
for(int i=1,x,y;i<n;i++)
{
cin>>x>>y;
addedge(x,y), addedge(y,x);
}
for(int i=1,x,y;i<=m;i++)
{
cin>>x>>y;
c[x] = y, root = x;
}
dfs(root);
cout<<f[root][c[root]]<<endl;
return 0;
}
40 分,后 6 个点全部 MLE。