import sys #input = sys.stdin.readline #文字列につけてはダメ input = sys.stdin.buffer.readline #文字列につけてはダメ #sys.setrecursionlimit(1000000) #import bisect #import itertools #import random #from heapq import heapify, heappop, heappush #from collections import defaultdict #from collections import deque #import copy #import math #from functools import lru_cache #@lru_cache(maxsize=None) MOD = pow(10,9) + 7 #MOD = 998244353 #dx = [1,0,-1,0] #dy = [0,1,0,-1] def main(): N = int(input()) G = [[] for _ in range(N)] for i in range(N-1): a,b,c = map(int,input().split()) a -= 1; b -= 1 G[a].append((b,c)); G[b].append((a,c)) ans = 0 for i in range(33): base = (1<>i)&1 == 1: uf.union(u,v) stack.append(u) used = set([]) for j in range(N): if uf.find(j) in used: continue used.add(uf.find(j)) #print(j,uf.find(j),uf.get_size(j)) num = uf.get_size(j) temp = num*(num-1)//2%MOD ans += base*temp%MOD ans %= MOD print(ans) 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): """ 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 と 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): """ x と y が同じグループか否か """ return self.find(x) == self.find(y) def get_size(self, x): """ x が属するグループの要素数 """ x = self.find(x) return self.size[x] if __name__ == '__main__': main()