class Integer def mod_inverse(mod) self.pow(mod - 2, mod) end end T = gets.to_i MOD = 10 ** 9 + 7 def mul(a, b) ret = Array.new(a.size) { Array.new(b[0].size, 0) } a.size.times do |i| b.size.times do |k| b[0].size.times do |j| ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]) % MOD end end end ret end def pow(a, n) h = a.size w = a[0].size ret = Array.new(h) { Array.new(w, 0) } h.times do |i| ret[i][i] = 1 end while n > 0 ret = mul(ret, a) if n % 2 == 1 a = mul(a, a) n /= 2 end ret end A = [ [1, 0, 0, 0, 1, 1], [0, 1, 0, 1, 0, 1], [0, 0, 1, 1, 1, 0], [3, 0, 0, 0, 0, 0], [0, 3, 0, 0, 0, 0], [0, 0, 3, 0, 0, 0], ] T.times do n = gets.to_i a = pow(A, n) m = 3.pow(n + 1, MOD).mod_inverse(MOD) b = mul(a, [[1], [0], [0], [3], [0], [0]]) puts m * b[3][0] % MOD end