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, B = gets.split.map(&:to_i) a = [[1, 0, 0], [0, 1, 0], [A, B, 0]] b = [[0, 0, 1], [1, 0, 0], [0, 0, 0]] c = mul(b, a) N = gets.to_i N.times do t = gets.to_i ret = pow(c, t / 2) if t.odd? ret = mul(a, ret) end if t == 0 puts 2 else puts ret.map(&:sum).sum % MOD end end