#include // clang-format off using namespace std;constexpr int INF=1001001001;constexpr long long INFll=1001001001001001001;namespace viewer{template string f(T i){string s=(i==INF||i==INFll?"inf":to_string(i));s=string(max(0,3-int(s.size())),' ')+s;return s;} templateauto v(T&x,string end)->decltype(cerr<void v(const pair&p,string end="\n"){cerr<<"(";v(p.first,", ");v(p.second,")"+end);}templatevoid v(const tuple&t,string end="\n"){auto[a,b]=t;cerr<<"(";v(a,", ");v(b,")"+end);}templatevoid v(const tuple&t,string end="\n"){auto[a,b,c]=t;cerr<<"(";v(a,", ");v(b,", ");v(c,")"+end);}templatevoid v(const tuple&t,string end="\n"){auto[a,b,c,d]=t;cerr<<"(";v(a,", ");v(b,", ");v(c,", ");v(d,")"+end);} templatevoid v(const vector&vx,string);templateauto ve(int,const vector&vx)->decltype(cerr<auto ve(bool,const vector &vx){cerr << "{\n";for(const T&x:vx)cerr<<" ",v(x,",");cerr<<"}\n";}templatevoid v(const vector&vx, string){ve(0,vx);}templatevoid v(const deque&s,string e){vectorz(s.begin(),s.end());v(z,e);} templatevoid v(const set&s,string e){vectorz(s.begin(),s.end());v(z,e);}templatevoid v(const multiset&s,string e){vectorz(s.begin(),s.end());v(z,e);}templatevoid v(const unordered_set&s,string e){vectorz(s.begin(),s.end());v(z,e);}templatevoid v(const priority_queue&p,string e){priority_queueq=p;vectorz;while(!q.empty()){z.push_back(q.top());q.pop();}v(z,e);} templatevoid v(const map&m,string e){cerr<<"{"<<(m.empty()?"":"\n");for(const auto&kv:m){cerr<<" [";v(kv.first,"");cerr<<"] : ";v(kv.second,"");cerr<<"\n";}cerr<<"}"+e;}templatevoid _view(int n,string s,T&var){cerr<<"\033[1;32m"<void grid(T _){}void grid(const vector>&vvb){cerr<<"\n";for(const vector&vb:vvb){for(const bool&b:vb)cerr<<(b?".":"#");cerr<<"\n";}} void _debug(int,string){}templatevoid _debug(int n,string S,H h,T... t){int i=0,cnt=0;for(;iap;mint re=a;for(long long r=1;r -sz[a] - 1 = edge[a] O(N) int num_of_tree(){int num_of_tree=0;for(int a=0;a<_n;a++)if(par_or_sz[a]<0&&-par_or_sz[a]>edge[a])num_of_tree++;return num_of_tree;} // the list of the "list of the vertices in a connected component" O(N) vector>groups(bool index1=false){vector>ret(_n);for(int i=0;i<_n;i++)ret[find(i)].push_back(i+int(index1));ret.erase(remove_if(ret.begin(),ret.end(),[&](const vector&v){return v.empty();}),ret.end());return ret;} // UnionFind::groups().size() O(1) int group_count(){return group_cnt;} protected: int _n;int group_cnt;/* root node: -1 * component size , otherwise: parent */vectorpar_or_sz;vectoredge; }; /// @ref https://qiita.com/drken/items/cce6fc5c579051e64fab /// @brief merge(a,b,w): A[a] - A[b] == z /// diff(a,b): A[b]-A[a] (REQUIRE:same(a,b)) template struct weighted_dsu : UnionFind { explicit weighted_dsu(int n,Abel SUM_UNITY=0):diff_weight(n,SUM_UNITY){_n=n;group_cnt=0;par_or_sz.resize(n,-1);} int find(int a)override{assert(0<=a&&a<_n);if(par_or_sz[a]<0)return a;int r=find(par_or_sz[a]);diff_weight[a]+=diff_weight[par_or_sz[a]];return par_or_sz[a]=r;} int merge(int a,int b,Abel w){assert(0<=a&&a<_n&&0<=b&&b<_n);w+=weight(a)-weight(b);int x=find(a),y=find(b);if(x==y)return x;group_cnt--;if(-par_or_sz[x]<-par_or_sz[y])swap(x,y),w=-w;par_or_sz[x]+=par_or_sz[y];diff_weight[y]=w;return par_or_sz[y]=x;} Abel weight(int a){find(a);return diff_weight[a];} Abel diff(int a, int b){assert(same(a,b));return weight(b)-weight(a);} private:vector diff_weight;}; /// @ref https://misteer.hatenablog.com/entry/persistentUF /// @ref https://ei1333.github.io/library/structure/union-find/partially-persistent-union-find.cpp /// @brief UnionFind which can reference all previous versions and update the current (not previous) version. /// @note t = -1 is the initial time.(each node's parent is itself) struct partially_persistent_dsu { partially_persistent_dsu(int n):_n(n),par_or_sz(n,-1),last(n,INF),add(n,{{-1,-1}}){} int merge(int a,int b,int t){assert(0<=a&&a<_n&&0<=b&&b<_n);int x=find(a,t),y=find(b,t);if(x==y)return x;if(-par_or_sz[x]<-par_or_sz[y])swap(x,y);last[y]=t;par_or_sz[x]+=par_or_sz[y];add[x].emplace_back(t,par_or_sz[x]);return par_or_sz[y]=x;} int find(int a,int t){assert(0<=a&&a<_n);if(tsecond;} private:int _n;vectorpar_or_sz;vectorlast;vector>>add;}; // clang-format on int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector> edges; for (int i = 0; i < N - 1; i++) { int a, b; long long c; cin >> a >> b >> c; a--; b--; edges.emplace_back(a, b, c); } mint ans = 0; for (int bit = 0; bit < 64; bit++) { mint ans2 = 0; UnionFind uf(N); for (auto &[a, b, c] : edges) { if (c & (1ll << bit)) { uf.merge(a, b); } } for (auto &g : uf.groups()) { ans2 += nCk(int(g.size()), 2); } ans2 *= mint(2).pow(bit); ans += ans2; } cout << ans << endl; return 0; }