#include #include #include using namespace atcoder; using mint = modint1000000007; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000001 struct HLD{ vector sz,parent,depth,root,pos; vector arr; HLD(vector> &E){ sz.resize(E.size(),1); parent.resize(E.size(),0); depth.resize(E.size(),0); root.resize(E.size(),0); pos.resize(E.size(),0); dfs(0,-1,E); dfs2(0,-1,E,0); } void dfs(int now,int p,vector> &E){ parent[now] = p; if(p==-1){ depth[now] = 0; } else{ depth[now] = depth[p]+1; } for(int i=0;i> &E,int r){ pos[now] = arr.size(); arr.push_back(now); root[now] = r; int maxi = 0; int ind = -1; for(int i=0;i> query(int u,int v){ vector> ret; int t = 0; while(root[u]!=root[v]){ if(depth[root[u]] <= depth[root[v]]){ ret.insert(ret.begin()+t,{pos[root[v]], pos[v]}); v = parent[root[v]]; } else{ ret.insert(ret.begin()+t,{pos[u],pos[root[u]]}); u = parent[root[u]]; t++; } } ret.insert(ret.begin()+t,{pos[u],pos[v]}); return ret; } int lca(int u,int v){ for(;;v=parent[root[v]]){ if(pos[u]>pos[v])swap(u,v); if(root[u]==root[v])return u; } } int get_distance(int u,int v){ return depth[u] + depth[v] - 2 * depth[lca(u,v)]; } }; int op(int a,int b){ return min(a,b); } int e(){ return Inf; } int mapping(int a,int b){ if(a==-1)return b; return a; } int composition(int a,int b){ if(a==-1)return b; if(b==-1)return a; return a; } int id(){ return -1; } int main(){ int N,M; cin>>N>>M; dsu D(N); vector A(M),B(M); rep(i,M){ scanf("%d %d",&A[i],&B[i]); A[i]--;B[i]--; } vector ind; vector> E(N); rep(i,M){ if(D.same(A[i],B[i]))continue; D.merge(A[i],B[i]); int t = E.size(); E[A[i]].push_back(t); E[B[i]].push_back(t); E.push_back({A[i],B[i]}); ind.push_back(i); } HLD H(E); vector cost(E.size(),0); vector f(E.size(),false); f[0] = true; { queue Q; Q.push(0); while(Q.size()>0){ int u = Q.front(); Q.pop(); rep(i,E[u].size()){ int v = E[u][i]; if(f[v])continue; f[v] = true; mint temp = cost[u]; if(v >= N){ temp += mint(2).pow(ind[v-N]); } cost[v] = temp; Q.push(v); } } } lazy_segtree seg(E.size()); for(int i=M-1;i>=0;i--){ if(binary_search(ind.begin(),ind.end(),i))continue; auto ret = H.query(A[i],B[i]); rep(j,ret.size()){ int l = ret[j].first,r = ret[j].second; if(l>r)swap(l,r); r++; seg.apply(l,r,i); } } /* rep(i,E.size()){ cout<>Q; rep(_,Q){ int x,y,z; scanf("%d %d %d",&x,&y,&z); x--;y--;z--; mint ans = 0; bool ng = false; if(!binary_search(ind.begin(),ind.end(),z)){ ans += cost[x] + cost[y]; ans -= cost[H.lca(x,y)] * 2; } else{ z = distance(ind.begin(),lower_bound(ind.begin(),ind.end(),z)); int pos = H.pos[N+z]; auto ret = H.query(x,y); bool ff = true; rep(j,ret.size()){ int l = ret[j].first,r = ret[j].second; if(l>r)swap(l,r); r++; if(l<=pos&&posr)swap(l,r); r++; if(l<=pos&&posH.get_distance(y,A[ttt])+H.get_distance(x,B[ttt])){ swap(x,y); } ans += cost[x] + cost[A[ttt]] - cost[H.lca(x,A[ttt])]*2; ans += cost[y] + cost[B[ttt]] - cost[H.lca(y,B[ttt])]*2; } } } ans *= 2; if(ng)printf("-1\n"); else printf("%d\n",ans.val()); } return 0; }