import sys def S(): return sys.stdin.readline().rstrip() def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LS(): return list(sys.stdin.readline().rstrip().split()) t = I() MOD = 10**9+7 for _ in range(t): n, ag, bg, ac, bc, ap, bp = MI() g1 = pow(bg-ag, n, MOD) g0 = pow(bg,n,MOD) g2 = pow(g0, MOD-2, MOD) c1 = pow(bc-ac, n, MOD) c0 = pow(bc,n,MOD) c2 = pow(c0, MOD-2, MOD) p1 = pow(bp-ap, n, MOD) p0 = pow(bp,n,MOD) p2 = pow(p0, MOD-2, MOD) ans = 1-(g1*g2)%MOD-(c1*c2)%MOD-(p1*p2)%MOD+2*pow(ag,n, MOD)*g2+2*pow(ac,n, MOD)*c2+2*pow(ap,n, MOD)*p2 ans %= MOD print(ans)