Centrifuge
시간 제한1.5초메모리 제한2048 MB
각 노드에 유체량이 주어진 트리에서 루트를 무작위로 고르고 바깥 방향으로 흐르며 각 분기에서 균등하게 나뉠 때 각 노드에 도달하는 유체량의 기댓값을 구한다.
문제
On your latest intergalactic scavenger trip, you discovered an abandoned futuristic robot. Mysteriously, the robot consists of ball-shaped joints with some kind of fluid inside. These joints are all connected together via flexible pipes that allow the fluid to flow bidirectionally from one joint to another. To analyze this strange fluid inside the robot, you decide to use a centrifuge.
Since you have no clue how to actually use a centrifuge, you secure one of the joints to the center of it and turn the machine on. The machine then rapidly spins around the center, and all the fluids are pushed away from the center towards the outside. Whenever the fluid has more than one pipe to flow through, the fluid splits evenly between all of these pipes.
You wonder how much fluid will be in the outermost joints at the end of this process. After thinking so much, you forgot which joint you secured to the center. Therefore, you have to calculate the expected amount of fluid in each joint as if you chose a joint at random.
입력
The first line contains one integer () --- the number of joints of the robot.
The next line contains integers () --- the amount of fluid in the -th joint.
The next lines contain two integers and () --- indicating a pipe between joint and . These pipes form a tree.
출력
Output lines with one integer per line, the -th representing the expected amount of fluid in the -th joint after the process modulo .
Formally, let . It can be shown that the answer can be expressed as an irreducible fraction , where and are integers and . Output the integer equal to . In other words, output such an integer that and .
힌트
In the first sample, if the first joint is fixed at the center, all fluid will flow from joint to , and the combined fluid will flow to joint . In total, joint and will be empty, and all units of fluid will be at joint .
If the second joint is fixed to the center, half of its fluid will flow to and half will flow to . There will be , , and units of fluid at each joint respectively. If joint is fixed, all units of fluid will be at joint .
In total, the expected amount of fluid in each joint is