# Enter your code here. Read input from STDIN. Print output to STDOUT a, b, t = gets.split.map(&:to_i) avg = (a + b) / 2 def expmod(n, exp, m) arr = [n] 59.times do arr << (arr.last * arr.last) % m end bits = (0..59).select { |b| exp[b] == 1 } bits.map { |b| arr[b] }.reduce(:+) % m end puts expmod(avg, t, 10 ** 9 + 7)