#include <bits/stdc++.h>
using namespace std;
#define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
#define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
#define cout_map(mp) for(auto& [f, s] : mp) cout << f << " " << s << "\n";
#define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
#define fixed(n) fixed << setprecision(n)
#define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
#define fill(vec, value) memset(vec, value, sizeof(vec));
#define Num_of_Digits(n) ((int)log10(n) + 1)
#define mod_combine(a, b, m) (((a % m) * (b % m)) % m)
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
#define sz(x) int(x.size())
#define debug(x) cout << #x << ": " << (x) << "\n";
#define fi first
#define se second
#define Pair pair < ll, ll >
#define ll long long
#define ull unsigned long long
#define Mod 1'000'000'007
#define OO 2'000'000'000
#define EPS 1e-9
#define PI acos(-1)
template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
for (auto &x: v) in >> x;
return in;
}
template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
for (const T &x: v) out << x << ' ';
return out;
}
void AhMeD_HoSSaM(){
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}
vector < vector < vector < Pair > > > tree(2, vector < vector < Pair > > (1e5 + 5));
vector < ll > Sz(1e5 + 10, 1);
ll bits[2][30][100'005];
void add_edge(int u, int v, ll w, int t){
tree[t][u].push_back({v, w}), tree[t][v].push_back({u, w});
}
void dfs(int u, int p, int t){
for(auto& [v, w] : tree[t][u]){
if(v == p) continue;
dfs(v, u, t);
Sz[u] += Sz[v];
for(int b = 0; b < 30; b++){
if(w & (1 << b))
bits[t][b][u] += Sz[v] - bits[t][b][v];
else
bits[t][b][u] += bits[t][b][v];
}
}
}
void dfs2(int u, int p, int t){
for(auto& [v, w] : tree[t][u]){
if(v == p) continue;
for(int b = 0; b < 30; b++){
int last_p = 0;
if(w & (1 << b))
last_p = Sz[v] - bits[t][b][v];
else
last_p = bits[t][b][v];
if(w & (1 << b))
bits[t][b][v] += (Sz[u] - Sz[v]) - (bits[t][b][u] - last_p);
else
bits[t][b][v] += bits[t][b][u] - last_p;
}
Sz[v] += Sz[u] - Sz[v];
dfs2(v, u, t);
}
}
void Solve(){
int n;
cin >> n;
fill(bits, 0);
for(int i = 0, u, v, w; i < n - 1 && cin >> u >> v >> w; i++) add_edge(u, v, w, 0);
for(int i = 0, u, v, w; i < n - 1 && cin >> u >> v >> w; i++) add_edge(u, v, w, 1);
dfs(1, 0, 0);
dfs2(1, 0, 0);
Sz.assign(1e5 + 10, 1);
dfs(1, 0, 1);
dfs2(1, 0, 1);
ll ans = 0;
for(int b = 0; b < 30; b++){
for(int u = 1; u <= n; u++){
ans += (((1 << b) % Mod) * ((bits[0][b][u] * (n - bits[1][b][u]))) % Mod) % Mod;
ans %= Mod;
ans += (((1 << b) % Mod) * ((bits[1][b][u] * (n - bits[0][b][u])) % Mod)) % Mod;
ans %= Mod;
}
}
cout << ans << '\n';
}
int main(){
AhMeD_HoSSaM();
int t = 1;
//cin >> t;
while(t--)
Solve();
return 0;
}
Copy