using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; using System.Threading; using System.Runtime.CompilerServices; using System.Diagnostics; //using nint=System.Int32; static class Program{ const int mod=(int)1e9+7; static void Main(){ Sc sc=new Sc(); var s=sc.Ia; var h=new Mint[s[0]]; var ba=new bool[s[0]]; for(int i = 0;iuf2.Size(b)){ p=h[a-1]; uf2.Scan(uf2.Root(b),uf2.Size(b),x=>{ba[x-1]=true;}); } else if(uf2.Size(a){ba[x-1]=true;}); } else{ //uf2.Union(e[0],e[1]); h[a-1]*=g; h[b-1]*=g; bo=false; } uf2.Union(a,b); if(bo){h[uf2.Root(a+1)-1]=p;} } } for(int i = 0;i0){ long t=a/b; a-=t*b;(a,b)=(b,a); u-=t*v;(u,v)=(v,u); } u%=mod; if(u<0){u+=mod;} return u%mod; } } public class Uf{ private int[] arr,hi; public int all; public Uf(int n){ arr=new int[n]; hi=new int[n]; all=n; for(int i=0;iarr[b]){(a,b)=(b,a);} arr[a]+=arr[b]; hi[a]=Max(hi[a],hi[b]+1); arr[b]=a; all--; l[a].ne=f[b]; l[a]=l[b]; return true; } return false; } public bool Same(int a,int b){return Root(a)==Root(b);} public int Size(int a){return -arr[Root(a)];} public int Root(int a){return arr[a]<0?a:arr[a]=Root(arr[a]);} public void Scan(int a,int m,Action fu){ var e=f[a]; for(int j = 0;j(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(){var s=Console.ReadLine().Split();return (Ct(s[0]),Ct(s[1]));} public (T,T,T) Tp3(){var s=Console.ReadLine().Split();return (Ct(s[0]),Ct(s[1]),Ct(s[2]));} public (T1,T1,T2) Tp3(){var s=Console.ReadLine().Split();return (Ct(s[0]),Ct(s[1]),Ct(s[2]));} private T Ct(string s){return (T)Convert.ChangeType(s,typeof(T));} }