import sys readline = sys.stdin.readline class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.rank[x] < self.rank[y]: x, y = y, x if self.rank[x] == self.rank[y]: self.rank[x] += 1 self.par[y] = x self.size[x] += self.size[y] def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] N = int(readline()) mod = 10 ** 9 + 7 A, B, C = [0] * (N - 1), [0] * (N - 1), [0] * (N - 1) for i in range(N - 1): A[i], B[i], C[i] = map(int, readline().split()) A[i], B[i] = A[i] - 1, B[i] - 1 ans = 0 for d in range(35): U = UnionFind(N) S = set() for i in range(N - 1): a, b, c = A[i], B[i], (C[i] >> d) & 1 if c == 0: continue U.union(a, b) for i in range(N): if U.find(i) in S: continue S.add(U.find(i)) sz = U.get_size(U.find(i)) ans += sz * (sz - 1) // 2 * (1 << d) ans %= mod print(ans)