class Fraction(): ##入力定義 def __init__(self,Numerator=0,Denominator=1): """分数のオブジェクトを生成する. Numerator:分子 Denominator:分母 """ self.a=Numerator self.b=Denominator #表示定義 def __str__(self): if self.b==1: return str(self.a) else: return "{}/{}".format(self.a,self.b) #四則演算定義 def __add__(self,other): c=Fraction() if not(isinstance(other,Fraction)): other=Fraction.Int_to_Fraction(other) c.a=self.a*other.b+self.b*other.a c.b=self.b*other.b return Fraction.__reduce(c) def __radd__(self,other): c=Fraction() if not(isinstance(other,Fraction)): other=Fraction.Int_to_Fraction(other) c.a=self.a*other.b+self.b*other.a c.b=self.b*other.b return Fraction.__reduce(c) def __sub__(self,other): return self+(-other) def __rsub__(self,other): return -self+other def __mul__(self,other): if not(isinstance(other,Fraction)): other=Fraction.Int_to_Fraction(other) return Fraction.__reduce(Fraction(self.a*other.a,self.b*other.b)) def __rmul__(self,other): if not(isinstance(other,Fraction)): other=Fraction.Int_to_Fraction(other) return Fraction.__reduce(Fraction(self.a*other.a,self.b*other.b)) def __floordiv__(self,other): if other==Fraction(): raise ZeroDivisionError H=self/other return H.a//H.b def __rfloordiv__(self,other): if self==Fraction(): raise ZeroDivisionError H=other/self return H.a//H.b def __truediv__(self,other): if other==Fraction(): raise ZeroDivisionError if not(isinstance(other,Fraction)): other=Fraction.Int_to_Fraction(other) return self*Fraction.__inverse(other) def __rtruediv__(self,other): if self==Fraction(): raise ZeroDivisionError if not(isinstance(self,Fraction)): self=Fraction.Int_to_Fraction(other) return Fraction.__inverse(self)*other def __pow__(self,other): if other==0: return 1 n=abs(other) g=1 k=self while n>0: if n%2: g*=k k*=k n>>=1 if other>0: return g else: return 1/g #丸め def __floor__(self): return self.a//self.b def __ceil__(self): return (self.a+self.b-1)//self.b #比較演算子 def __eq__(self,other): t=self-other if isinstance(t,Fraction): return t.a==0 else: return t==0 def __nq(self,other): return not(self==other) def __lt__(self,other): t=self-other if isinstance(t,Fraction): return t.a<0 else: return t<0 def __le__(self,other): return (self0:return 1 elif s==0:return 0 else:return -1 def __reduce(self): from math import gcd g=gcd(self.a,self.b) self.a//=g self.b//=g if self.b<0: self.a*=-1 self.b*=-1 return Fraction(self.a,self.b) def Int_to_Fraction(self): if not(isinstance(self,Fraction)): return Fraction(self,1) else:return self def __inverse(self): if self==Fraction(): raise ZeroDivisionError return Fraction.__reduce(Fraction(self.b,self.a)) def __pos__(self): return self def __neg__(self): return Fraction(-self.a,self.b) def __abs__(self): return max(self,-self) #その他 def is_unit(self): return ((self.b)%(self.a))==0 #================================================ M=10**9+7 T=int(input()) assert 1<=T<=10**4,"Tが制約外(T={})".format(T) H=[] for i in range(T): U=list(map(int,input().split())) N=U[0] A=(U[1]*pow(U[2],M-2,M))%M B=(U[3]*pow(U[4],M-2,M))%M C=(U[5]*pow(U[6],M-2,M))%M assert 2<=N<=10**18,"第iテストケースのNが制約外(N={})".format(N) assert 0<=U[1]<=U[2]<=10**9,"第{}テストケースのグーが制約外(A_G={},B_G={})".format(i+1,U[1],U[2]) assert 0<=U[3]<=U[4]<=10**9,"第{}テストケースのチョキが制約外(A_C={},B_C={})".format(i+1,U[3],U[4]) assert 0<=U[5]<=U[6]<=10**9,"第{}テストケースのパーが制約外(A_P={},B_P={})".format(i+1,U[5],U[6]) assert U[2]!=0,"第{}テストケースのグーの分母が0".format(i+1) assert U[4]!=0,"第{}テストケースのチョキの分母が0".format(i+1) assert U[6]!=0,"第{}テストケースのパーの分母が0".format(i+1) p,q,r=Fraction(U[1],U[2]),Fraction(U[3],U[4]),Fraction(U[5],U[6]) assert p+q+r==1,"第{}テストケースの確率の和が1ではない.(確率の和={})".format(i+1,p+q+r) X=(pow(1-A,N,M)+pow(1-B,N,M)+pow(1-C,N,M))%M Y=(pow(A,N,M)+pow(B,N,M)+pow(C,N,M))%M H.append((1-X+2*Y)%M) print("\n".join(map(str,H)))