constint maxn=100010; int n,tot,head[maxn],a[maxn],k; typedeflonglong ll; ll f[maxn][4]; const ll mod=1000000007; structnode { int nxt,to; }edge[maxn<<1];
intread() { int x=0,ff=1; char c=getchar(); while (c<48||c>57) ff=c=='-'?-1:1,c=getchar(); while (c>=48&&c<=57) x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*ff; }
voiddp(int u,int fa) { int i,v,p=0; f[u][1]=f[u][2]=f[u][3]=1; if (a[u]) { f[u][1]=f[u][2]=f[u][3]=0; f[u][a[u]]=1; } for (i=head[u];i;i=edge[i].nxt) { v=edge[i].to; if (v==fa) continue; dp(v,u); p=1; if (a[u]==0) {//注意这里是先+后* f[u][1]=(f[u][1]*(f[v][2]+f[v][3])%mod)%mod; f[u][2]=(f[u][2]*(f[v][1]+f[v][3])%mod)%mod; f[u][3]=(f[u][3]*(f[v][1]+f[v][2])%mod)%mod; } else f[u][a[u]]=(f[u][a[u]]*(f[v][1]+f[v][2]+f[v][3]-f[v][a[u]])%mod+mod)%mod; } }