main = -> { N = gets.to_i edges = Array.new(N - 1) { a, b, c = gets.split.map(&:to_i); [a - 1, b - 1, c] } ans = 0 (0 .. 30).each do |bit| uf = UnionFind.new(N) edges.each do |a, b, c| uf.merge(a, b) if c[bit] == 1 end leaders = (0 ... N).map { uf.leader(_1) }.uniq ans += leaders.sum { |i| k = uf.size(i); k * (k - 1) / 2 } % MOD * 2.pow(bit, MOD) ans %= MOD end puts ans } MOD = 10**9 + 7 class UnionFind def initialize(size); @p = Array.new(size, -1); end def leader(i); j = i; j, i = i, @p[j] = @p[i] until @p[i] < 0; i; end def merge(i, j); k, l = leader(i), leader(j); return false if k == l; k, l = l, k if @p[k] > @p[l]; @p[k] += @p[l]; @p[l] = k; true; end def same?(i, j); leader(i) == leader(j); end def size(i); -@p[leader(i)]; end end main.call