class Tree(): def __init__(self, n): self.n = n self.tree = [[] for _ in range(n)] self.cost = dict() self.root = None def add_edge(self, u, v, c): if u > v: u, v = v, u self.tree[u].append(v) self.tree[v].append(u) self.cost[u * self.n + v] = c def get_cost(self, u, v): if u < v: return self.cost[u * self.n + v] else: return self.cost[v * self.n + u] def set_root(self, r=0): self.root = r self.par = [None] * self.n self.ord = [r] stack = [r] while stack: v = stack.pop() for adj in self.tree[v]: if self.par[v] == adj: continue 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 in self.tree[v]: if self.par[v] == adj: continue lt[adj] = tl self.w = self.get_cost(v, adj) tl = op(tl, dp[adj]) for adj in self.tree[v][::-1]: if self.par[v] == adj: continue rt[adj] = tr self.w = self.get_cost(v, adj) tr = op(tr, dp[adj]) dp[v] = tr for v in self.ord: if v == self.root: continue p = self.par[v] if p != self.root: pp = self.par[p] self.w = self.get_cost(p, pp) inv[v] = op(merge(lt[v], rt[v]), inv[p]) else: inv[v] = merge(lt[v], rt[v]) self.w = self.get_cost(v, p) dp[v] = op(dp[v], inv[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) e = (1, 0, 0) def op(p, c): ps, pd, pds = p cs, cd, cds = c size = ps + cs w = t.w dist = pd + cd + w * cs dsq = (pds + cds + w**2 * cs + 2 * w * cd) % MOD return size, dist, dsq id = (0, 0, 0) def merge(lt, rt): ls, ld, lds = lt rs, rd, rds = rt size = ls + rs - 1 dist = ld + rd dsq = (lds + rds) % MOD return size, dist, dsq dp = t.rerooting(op, e, merge, id) res = 0 for size, dist, dsq in dp: res += dsq res %= MOD print(res * pow(2, MOD - 2, MOD) % MOD)