// Arup Guha
// 12/31/2017
// Solution to 2017 Dec USACO Gold Problem: Barn Painting

import java.util.*;
import java.io.*;

public class barnpainting {

	final public static long MOD = 1000000007L;

	public static int n;
	public static int k;
	public static int[] colors;
	public static ArrayList[] g;
	public static int[] par;
	public static long[][] dp;

	public static void main(String[] args) throws Exception {

		// Read in n, k, set up data structs.
		BufferedReader stdin = new BufferedReader(new FileReader("barnpainting.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		n = Integer.parseInt(tok.nextToken());
		k = Integer.parseInt(tok.nextToken());
		colors = new int[n];
		g = new ArrayList[n];
		for (int i=0; i<n; i++)
			g[i] = new ArrayList<Integer>();
		par = new int[n];
		dp = new long[4][n];

		// Add edges.
		for (int i=0; i<n-1; i++) {
			tok = new StringTokenizer(stdin.readLine());
			int v1 = Integer.parseInt(tok.nextToken())-1;
			int v2 = Integer.parseInt(tok.nextToken())-1;
			g[v1].add(v2);
			g[v2].add(v1);
		}

		// Add known colors.
		for (int i=0; i<k; i++) {
			tok = new StringTokenizer(stdin.readLine());
			int v1 = Integer.parseInt(tok.nextToken())-1;
			int c = Integer.parseInt(tok.nextToken());
			colors[v1] = c;
		}

		// Ta da!
		PrintWriter out = new PrintWriter(new FileWriter("barnpainting.out"));
		out.println(solve());
		out.close();
		stdin.close();
	}

	public static long solve() {

		// Take out really easy cases.
		for (int i=0; i<n; i++) {
			for (Integer j : (ArrayList<Integer>)g[i]) {
				if (colors[i] == colors[j] && colors[i] != 0)
					return 0L;
			}
		}

		// Root tree at 0.
		int[] bfsOrder = new int[n];
		boolean[] visited = new boolean[n];
		visited[0] = true;
		Arrays.fill(par, -1);
		LinkedList<Integer> q = new LinkedList<Integer>();
		q.offer(0);
		int ID = 0;

		// run bfs to get order of vertices.
		while (q.size() > 0) {

			int cur = q.poll();
			bfsOrder[ID++] = cur;

			// Go to neighbors.
			for (Integer next : (ArrayList<Integer>)g[cur]) {
				if (visited[next]) continue;
				q.offer(next);
				par[next] = cur;
				visited[next] = true;
			}
		}

		// Do DP here.
		for (int i=n-1; i>=0; i--) {

			// Vertex we will solve the problem for.
			int v = bfsOrder[i];

			// Must do all colors.
			for (int j=1; j<=3; j++) {

				// Forced node and j+1 doesn't match its color.
				if (colors[v] != 0 && colors[v] != j) continue;

				long res = 1L;

				// Go through kids.
				for (Integer next: (ArrayList<Integer>)g[v]) {

					if (next == par[v]) continue;

					// Find all ways to color this child any color but j.
					res = (res*(dp[0][next]-dp[j][next]+MOD))%MOD;
				}

				dp[j][v] = res;
			}

			dp[0][v] = (dp[1][v] + dp[2][v] + dp[3][v])%MOD;

		}

		return dp[0][0];
	}
}

