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

hide comments
dung04052000: 2016-08-13 16:14:56

Hi
wasn't the tests from this site ? http://www.hsin.hr/2008/final/first_day/tasks.pdf from http://www.hsin.hr/2008/
I ran it on m computer and got the right answer on test 10 but got WA in here, can somebody explain why ?
my code :https://ideone.com/ZvoeWs , is it because of my computer ?

aghori_sadhu: 2016-07-14 15:21:39

Cool problem :)

linkret: 2016-06-29 09:25:19

Tigran Galstyan, COI is Croatian Olimpiad in Informatics

Jens Stimpfle: 2014-07-15 22:04:01

Warning for those parsing integers themselves: Input file has DOS line endings.

Tigran Galstyan: 2014-04-24 10:36:08

Can anyone tell me what contest is COI??

Igor: 2013-07-02 18:12:13

Can't figure out what is wrong with my solution. Also wrote an O(N^3) bruteforce solver and results match. Could anyone look at my submission 9592767?

guliashvili: 2013-02-09 16:19:03

Srivatsan B
on servers programs have bigger stack.

Shinken Yellow: 2012-06-08 16:43:36

I am Vietnamese.

mike!: 2012-06-04 23:31:26

Last edit: 2012-06-16 17:36:15

Added by:~!(*(@*!@^&
Date:2009-02-28
Time limit:0.113s-0.226s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:COI 08