class UnionFind(object): def __init__(self, size): self.parent = [-1] * size def root(self, x): if self.parent[x] < 0: return x self.parent[x] = self.root(self.parent[x]) return self.parent[x] def union(self, x, y): x, y = self.root(x), self.root(y) if x == y: return False if self.parent[x] > self.parent[y]: x, y = y, x self.parent[x] += self.parent[y] self.parent[y] = x return True def size(self, x): return -self.parent[self.root(x)] N = int(input()) data = [] for _ in range(N-1): data.append(tuple(map(int, input().split()))) ans = 0 mod = 10**9+7 for k in range(30): uf = UnionFind(N) for a, b, c in data: if c >> k & 1: uf.union(a-1, b-1) for r in range(N): if uf.parent[r] < 0: ans += (1 << k) * uf.size(r) * (uf.size(r) - 1) // 2 ans %= mod print(ans)