#include <bits/stdc++.h>

#define mp std::make_pair
#define pb push_back
#define fr first
#define se second

typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef long long ll;

template<class T> inline T max(const T &x, const T &y) { return x > y ? x : y; }
template<class T> inline T min(const T &x, const T &y) { return x < y ? x : y; }
template<class T> inline bool chkmax(T &x, const T &y) { return x < y ? x = y, true : false; }
template<class T> inline bool chkmin(T &x, const T &y) { return x > y ? x = y, true : false; }

inline int read() {
  register int x = 0, v = 1;
  register char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') v = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * v;
}

const int MAXN = 2e5;

int n, pt1, pt2, tim1, tim2;
int fa[22][MAXN | 1], par[MAXN | 1];
int dfn1[MAXN | 1], dfn2[MAXN | 1], size[MAXN | 1], dep[MAXN | 1];
int ans[MAXN | 1];
vi e[MAXN | 1];
pii a[MAXN | 1];

int find(int x) {
  static int dep[MAXN | 1];
  std::queue<int> q;
  memset(dep, 0, sizeof(dep));
  int res = 0;
  q.push(x);
  dep[x] = 1;
  while (!q.empty()) {
    int frm = q.front(); q.pop();
    for (vi::iterator it = e[frm].begin(); it != e[frm].end(); ++it) {
      int to = *it;
      if (dep[to]) continue;
      dep[to] = dep[frm] + 1;
      q.push(to);
      if (dep[to] > dep[res]) res = to;
    }
  }
  return res;
}

void dfs1(int x, int par) {
  fa[0][x] = par; dep[x] = dep[par] + 1;
  dfn1[x] = ++tim1; size[x] = 1;
  for (int i = 1; i <= 20; ++i) fa[i][x] = fa[i - 1][fa[i - 1][x]];
  for (vi::iterator it = e[x].begin(); it != e[x].end(); ++it) {
    int to = *it;
    if (to == par) continue;
    dfs1(to, x);
    size[x] += size[to];
  }
}

void dfs2(int x, int fa) {
  dfn2[x] = ++tim2;
  for (vi::iterator it = e[x].begin(); it != e[x].end(); ++it) {
    int to = *it;
    if (to == fa) continue;
    if (dfn1[to] <= dfn1[pt2] && dfn1[to] + size[to] - 1 >= dfn1[pt2]) continue;
    dfs2(to, x);
  }
  for (vi::iterator it = e[x].begin(); it != e[x].end(); ++it) {
    int to = *it;
    if (to == fa) continue;
    if (!(dfn1[to] <= dfn1[pt2] && dfn1[to] + size[to] - 1 >= dfn1[pt2])) continue;
    dfs2(to, x);
  }
}

int lca(int x, int y) {
  if (dep[x] < dep[y]) std::swap(x, y);
  for (int i = 20; ~i; --i)
    if (dep[fa[i][x]] >= dep[y]) x = fa[i][x];
  if (x == y) return x;
  for (int i = 20; ~i; --i)
    if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
  return fa[0][x];
}

int dist(int x, int y) {
  if (!y) return 0;
  int res = 0, lx = x, ly = y, Lca = lca(x, y);
  return dep[x] + dep[y] - 2 * dep[Lca];
}

int main() {
  n = read();
  for (int i = 1; i < n; ++i) {
    int u = read(), v = read();
    e[u].pb(v); e[v].pb(u);
  }
  pt1 = find(1); 
  pt2 = find(pt1);
  dfs1(pt1, 0);
  dfs2(pt1, 0);
  for (int i = 1; i <= n; ++i) a[i].fr = dfn2[i], a[i].se = i;
  std::sort(a + 1, a + 1 + n);
  for (int i = 1, res = 1; i <= n; ++i) {
    res += dist(a[i].se, a[i - 1].se);
    ans[a[i].se] = res;
  }
  for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
  return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;


#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);  
#define ff              first
#define ss              second
#define int             long long
#define ll              long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zerobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define vpii            vector<pair<int,int> >
#define all(x)          x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y)   for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"
#define sz(x)           (int)x.size()
#define db(x)           cout<<x<<"\n";

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

//insert,find_by_order,order_of_key,lower_bound,upper_bound;

#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif

  
//////////////////////////////code//////////////////////////////

const int N = 2e5 + 5;


int ans , n;
int dp[N];
vpii adj[N];

void dfs(int node,int p,int w)
{
      dp[node] = w;

      for(auto &c :adj[node])
      {
            if(c.ff == p)
              continue;

            dfs(c.ff , node , c.ss ^ dp[node]);

      }
}


void solve()
{
      cin >> n;

      for(int i = 0;i < n - 1; i++)
      {
              int u , v , w;
              cin >> u >> v >> w;
              adj[u].pb({v , w});
              adj[v].pb({u , w});
      }


      ans = 0;

      dfs(1 , -1 , 0);

      int bit[62] = {};

      for(int i = 1;i <= n ; i++)
      {
              int num = dp[i];
              int j = 0;

              while(num)
              {
                    if(num&1)
                      bit[j]++;

                    num>>=1;
                    j++;
              }
      }

      int curr = 1;

      for(int i = 0;i < 62 ; i++)
      {
            int now = bit[i];
            now *= (n - bit[i]);
            now%=mod;
            int p = curr%mod;
            now*=p;
            now%=mod;
            ans += now;
            ans%=mod;

           // deb(ans, now);

            curr<<=1;
      }

      db(ans)

      
  

}

int32_t main()
{   

    LETS_GET_SCHWIFTY;

     #ifndef ONLINE_JUDGE
     freopen("INP.txt", "r", stdin);
     freopen("out.txt", "w", stdout);
     #endif  


    int t = 1;
   //cin >> t;

    while (t--)
        solve();

}

// check out for following mistakes-
// if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only
// is using prime sieve make sure it fits
// when using factorial template or combinatorics make sure that you edit fillfac fun values and array values

