Barn Painting

Count the proper 3-colorings of a tree consistent with some pre-colored nodes, modulo 1e9+7.

Medium6TreeDynamic programmingDFSCombinatoricsInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Farmer John has a farm with NN barns (1N1051 \le N \le 10^5). Some of them are already painted and the rest are not. John wants to paint the remaining barns so that every barn on the farm has a color, but he only has three paint colors. His prize cow Bessie gets confused when two barns joined by a direct path carry the same color, so John has to avoid that.

The paths joining the NN barns contain no cycle. For any two barns there is at most one sequence of paths leading from one to the other.

How many ways can John paint the barns that are still unpainted?

Input

The first line contains two integers NN and KK, the number of barns on the farm and the number of barns that are already painted (0KN0 \le K \le N).

Each of the next N1N-1 lines contains two integers xx and yy, describing a path that directly connects barn xx and barn yy (1x,yN1 \le x, y \le N, xyx \neq y).

Each of the next KK lines contains two integers bb and cc, meaning that barn bb is painted with color cc (1bN1 \le b \le N, 1c31 \le c \le 3). No barn number appears more than once in these lines.

Output

Print the number of ways to paint the remaining barns so that no two directly connected barns share a color, modulo 109+710^9 + 7.