MTREE - Another Tree Problem

As you are bound to know by now, a tree is a connected graph consisting of N vertices and N−1 edges. Trees also have the property of there being exactly a single unique path between any pair of vertices. You will be given a tree in which every edge is assigned a weight – a non negative integer. The weight of a path is the product of the weights of all edges on the path. The weight of the tree is the sum of the weights of all paths in the tree. Paths going in opposite directions (A to B and B to A) are considered the same and, when calculating the weight of a tree, are counted only once.

Write a program that, given a tree, calculates its weight modulo 1000000007.

Input

The first line contains the integer N (2 ≤ N ≤ 100 000), the number of vertices in the tree. The vertices are numbered 1 to N. Each of the following N−1 contains three integers A, B and W (1 ≤ A, B ≤ N, 0 ≤ W ≤ 1000) describing one edge. The edge connects vertices A and B, and its weight is W.

Output

Output the weight of the tree, modulo 1000000007.

Sample

input:
3
3 2 100
2 1 100

output:
10200
input:
4
1 2 5
1 3 5
1 4 5

output:
90
input:
5
1 2 2
2 3 3
4 3 2
5 3 2

output:
55

Được gửi lên bởi:psetter
Ngày:2009-02-28
Thời gian chạy:1s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:COI 08

hide comments
2021-05-27 18:02:11
Tham khảo: https://vnspoj.github.io/problems/MTREE
2018-02-25 18:18:11 Sơn Tùng M-TP
Gợi ý chút cho những bạn muốn xem: Sum of Products in a root node + tính toán cộng dồn theo từng node. Cẩn thận các phép toán số học.
2017-12-28 03:18:00
Quên cái long long mất mịa 1 đấm =.=
frostpixel aka.How 2 AC
2017-12-25 02:16:15
ledacthuongvq
2017-12-17 10:56:53
AE mod cẩn thận :))
2017-12-07 03:35:16
nhật hào sạch
2017-05-28 04:41:52


Last edit: 2017-05-28 04:42:25
2017-03-12 10:13:28
ai làm được r chỉ với
2015-09-25 19:05:33 Nguyễn Vĩnh Thịnh
cái mod kia làm khó quá T.T
2009-03-06 23:30:59 dhkhtn
Can than phep toan so hoc.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.