#include using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } template struct ModInt{ long long x; ModInt():x(0){} ModInt(long long y):x(y>=0?y%Mod:(Mod-(-y)%Mod)%Mod){} ModInt &operator+=(const ModInt &p){ if((x+=p.x)>=Mod) x-=Mod; return *this; } ModInt &operator-=(const ModInt &p){ if((x+=Mod-p.x)>=Mod)x-=Mod; return *this; } ModInt &operator*=(const ModInt &p){ x=(int)(1ll*x*p.x%Mod); return *this; } ModInt &operator/=(const ModInt &p){ (*this)*=p.inverse(); return *this; } ModInt operator-()const{return ModInt(-x);} ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;} ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;} ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;} ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;} bool operator==(const ModInt &p)const{return x==p.x;} bool operator!=(const ModInt &p)const{return x!=p.x;} ModInt inverse()const{ int a=x,b=Mod,u=1,v=0,t; while(b>0){ t=a/b; swap(a-=t*b,b);swap(u-=t*v,v); } return ModInt(u); } ModInt pow(long long n)const{ ModInt ret(1),mul(x); while(n>0){ if(n&1) ret*=mul; mul*=mul;n>>=1; } return ret; } friend ostream &operator<<(ostream &os,const ModInt &p){return os<>(istream &is,ModInt &a){long long t;is>>t;a=ModInt(t);return (is);} static int get_mod(){return Mod;} }; // 経路圧縮なし,マージテクでunite,クエリ毎O(logN) struct UnionFindUndo{ int con; vector data; stack> history; UnionFindUndo(int sz){ con=sz; data.assign(sz,-1); } bool unite(int x,int y){ x=root(x),y=root(y); if(x==y){ history.push(make_tuple(x,data[x],false)); history.push(make_tuple(y,data[y],false)); return false; } con--; history.push(make_tuple(x,data[x],true)); history.push(make_tuple(y,data[y],false)); if(data[x]>data[y]) swap(x,y); data[x]+=data[y]; data[y]=x; return true; } int root(int k){return (data[k]<0?k:(root(data[k])));} int size(int k){return (-data[root(k)]);} bool sameroot(int x,int y){return root(x)==root(y);} void undo(){ { auto [fs,sc,con_flag]=history.top();history.pop(); if(con_flag) con++; data[fs]=sc; } { auto [fs,sc,con_flag]=history.top();history.pop(); if(con_flag) con++; data[fs]=sc; } } void snapshot(){while(!history.empty())history.pop();} void rollback(){while(!history.empty())undo();} }; // verified https://codeforces.com/gym/100551/submission/103148848 // ref https://ei1333.hateblo.jp/entry/2017/12/14/000000 struct DynamicConnectivityOffline{ private: int sz; using P=pair; vector> seg; vector> es;// {{start idx, end idx}, {u, v}} map cnt,prev; void add_seg(int a,int b,P &e,int k,int l,int r){ if(r<=a or b<=l) return ; if(a<=l and r<=b){ seg[k].push_back(e); return ; } add_seg(a,b,e,2*k,l,(l+r)/2); add_seg(a,b,e,2*k+1,(l+r)/2,r); } public: int n,q; UnionFindUndo uf; DynamicConnectivityOffline(int n,int q):n(n),q(q),uf(n){ sz=1;while(szv) swap(u,v); if(cnt[P(u,v)]==0) prev[P(u,v)]=query_idx; cnt[P(u,v)]++; } void delete_edge(int query_idx,int u,int v){ if(u>v) swap(u,v); assert(cnt[P(u,v)]>0); cnt[P(u,v)]--; if(cnt[P(u,v)]==0) es.emplace_back(P(prev[P(u,v)],query_idx),P(u,v)); } void build(){ for(auto [e,c]:cnt)if(c>0) es.emplace_back(P(prev[e],sz),e); for(auto [t,e]:es) add_seg(t.first,t.second,e,1,0,sz); } // dfs -> segment tree // f(query_idx) void execute(const function &f,int k=1){ for(auto [u,v]:seg[k]) uf.unite(u,v); if(k; mint i2=mint(2).inverse(); signed main(){ int N;cin>>N; DynamicConnectivityOffline daicon(N,3*N-3); vector> E; rep(i,N-1){ int u,v;cin>>u>>v; E.emplace_back(u,v); daicon.add_edge(i,u,v); } rep(i,N-1){ auto [u,v]=E[i]; daicon.delete_edge(N-1+2*i,u,v); daicon.add_edge(N-1+2*i+1,u,v); } daicon.build(); mint num=0; auto f=[&](int k){ if(k