class Tree(): def __init__(self, n): self.n = n self.tree = [[] for _ in range(n)] self.root = None def add_edge(self, u, v, c): if u > v: u, v = v, u self.tree[u].append((v, c)) self.tree[v].append((u, c)) def set_root(self, r=0): self.root = r self.par = [None] * self.n self.ord = [r] self.cost = [0] * N stack = [r] while stack: v = stack.pop() for adj, cost in self.tree[v]: if self.par[v] == adj: continue self.cost[adj] = cost self.par[adj] = v self.ord.append(adj) stack.append(adj) def rerooting(self, op, e, merge, id): if self.root is None: self.set_root() dp = [e] * self.n lt = [id] * self.n rt = [id] * self.n inv = [id] * self.n for v in self.ord[::-1]: tl = tr = e for adj, cost in self.tree[v]: if self.par[v] == adj: continue lt[adj] = tl tl = op(tl, dp[adj], cost) for adj, cost in self.tree[v][::-1]: if self.par[v] == adj: continue rt[adj] = tr tr = op(tr, dp[adj], cost) dp[v] = tr for v in self.ord: if v == self.root: continue p = self.par[v] inv[v] = op(merge(lt[v], rt[v]), inv[p], self.cost[p]) dp[v] = op(dp[v], inv[v], self.cost[v]) return dp import io, os input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline MOD = 1000000007 N = int(input()) t = Tree(N) for _ in range(N - 1): u, v, w = map(int, input().split()) u -= 1; v -= 1 t.add_edge(u, v, w) def op(p, v, d): p_size, p_dist, p_square = p v_size, v_dist, v_square = v size = p_size + v_size dist = (p_dist + v_dist + d * v_size) % MOD square = (p_square + v_square + d**2 * v_size + 2 * d * v_dist) % MOD return size, dist, square def merge(lt, rt): l_size, l_dist, l_square = lt r_size, r_dist, r_square = rt size = l_size + r_size - 1 dist = (l_dist + r_dist) % MOD square = (l_square + r_square) % MOD return size, dist, square dp = t.rerooting(op, (1, 0, 0), merge, (0, 0, 0)) res = 0 for size, dist, square in dp: res += square res %= MOD print(res * pow(2, MOD - 2, MOD) % MOD)