class Integer def mod_inverse(mod) self.pow(mod - 2, mod) end end T = gets.to_i MOD = 10 ** 9 + 7 def f(n, a, b) c = (a + b) a1 = a.numerator.pow(n, MOD) a2 = a.denominator.pow(n, MOD) b1 = b.numerator.pow(n, MOD) b2 = b.denominator.pow(n, MOD) c1 = c.numerator.pow(n, MOD) c2 = c.denominator.pow(n, MOD) c1 * c2.mod_inverse(MOD) - a1 * a2.mod_inverse(MOD) - b1 * b2.mod_inverse(MOD) end T.times do n, ag, bg, ac, bc, ap, bp = gets.split.map(&:to_i) a = 3.pow(n, MOD).mod_inverse(MOD) g_rate = Rational(ag, bg) c_rate = Rational(ac, bc) p_rate = Rational(ap, bp) r1 = f(n, g_rate, c_rate) r2 = f(n, g_rate, p_rate) r3 = f(n, c_rate, p_rate) puts (1 - r1 - r2 - r3) % MOD end