import queue MOD = 1000000007 n = int(input()) edges = [map(int, input().split()) for _ in range(n - 1)] graph = [[] for _ in range(n)] for [a, b, c] in edges: graph[a - 1].append([b - 1, c]) graph[b - 1].append([a - 1, c]) depth = [-1] * n depth[0] = 0 queue = queue.Queue() queue.put(0) while not queue.empty(): top = queue.get() for [to, _] in graph[top]: if depth[to] == -1: depth[to] = depth[top] + 1 queue.put(to) fromLeaf = sorted([i for i in range(n)], key = lambda i: depth[i], reverse = True) result = 0 for d in range(30): onePath = 0 endPath = [0] * n for node in fromLeaf: count = 0 for [child, c] in graph[node]: if depth[child] <= depth[node] or ((c >> d) & 1) == 0: continue onePath += (count + 1) * (endPath[child] + 1) count += endPath[child] + 1 endPath[node] = count result += onePath % MOD << d print(result % MOD)