N = gets.to_i C, D = (1..N).map {gets.split.map(&:to_i)}.transpose M = 10**9 + 7 def f(a, n) if n == 0 [[1, 0], [0, 1]] else b = f(a, n / 2) c = [[(b[0][0] * b[0][0] + b[0][1] * b[1][0]) % M, (b[0][0] * b[0][1] + b[0][1] * b[1][1]) % M], [(b[1][0] * b[0][0] + b[1][1] * b[1][0]) % M, (b[1][0] * b[0][1] + b[1][1] * b[1][1]) % M]] if n % 2 == 0 c else [[(c[0][0] * a[0][0] + c[0][1] * a[1][0]) % M, (c[0][0] * a[0][1] + c[0][1] * a[1][1]) % M], [(c[1][0] * a[0][0] + c[1][1] * a[1][0]) % M, (c[1][0] * a[0][1] + c[1][1] * a[1][1]) % M]] end end end def g(a, n) if n == 0 1 else b = g(a, n / 2) c = b**2 % M if n % 2 == 0 c else (c * a) % M end end end ans = C.zip(D).map {|c, d| x = if c == 1 2 elsif c == 2 3 else a = f([[1, 1], [1, 0]], c - 2) 3 * a[0][0] + 2 * a[0][1] end g(x, d) }.inject(1) {|memo, item| (memo * item) % M} puts ans