Who made the Christmas sound

Root the tree at socket 1 and count colorings with R, G, B bulbs, respecting the green and blue adjacency rules, whose total cost is divisible by K.

Hard8Dynamic programmingTreeDFSCombinatoricsNo attempts yetTime limit1sMemory limit512 MB

Problem

Christmas is close, so Semter decorates a tree. The tree carries VV sockets for bulbs, and the sockets are joined by V1V-1 wires. Starting at socket 1 and following the wires, electricity reaches every socket, so the sockets and the wires form a tree.

Each socket takes exactly one bulb, red (RR), green (GG), or blue (BB). A red bulb in socket ii costs RiR_i, a green bulb costs GiG_i, and a blue bulb costs BiB_i. An arrangement must satisfy two conditions.

  • No wire directly joins two sockets that both hold a green bulb.
  • Every socket directly joined to a socket holding a blue bulb holds a green bulb.

Count the arrangements whose total cost over all sockets is a multiple of KK. The count can be very large, so print it modulo QQ. Two arrangements are different when at least one socket holds a bulb of a different color.

1V10001 \le V \le 1000, 1Q10001 \le Q \le 1000, 1K101 \le K \le 10, and 0Ri,Gi,Bi100 \le R_i, G_i, B_i \le 10.

Input

The first line has VV, QQ, and KK, separated by spaces.

Each of the next V1V-1 lines has two socket numbers aa and bb, separated by a space, meaning a wire joins socket aa and socket bb.

Each of the following VV lines has the costs RiR_i, GiG_i, and BiB_i of socket ii, in that order. Sockets are numbered 1 to VV.

Output

Print one line with the number of arrangements whose total cost is divisible by KK, taken modulo QQ.