Christmas is close, so Semter decorates a tree. The tree carries V sockets for bulbs, and the sockets are joined by V−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 (R), green (G), or blue (B). A red bulb in socket i costs Ri, a green bulb costs Gi, and a blue bulb costs Bi. 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 K. The count can be very large, so print it modulo Q. Two arrangements are different when at least one socket holds a bulb of a different color.
1≤V≤1000, 1≤Q≤1000, 1≤K≤10, and 0≤Ri,Gi,Bi≤10.