from math import isqrt mod = 1000000007 def mat_mul(a, b) : I, J, K = len(a), len(b[0]), len(b) c = [[0] * J for _ in range(I)] for i in range(I) : for j in range(J) : for k in range(K) : c[i][j] += a[i][k] * b[k][j] c[i][j] %= mod return c def mat_pow(x, n): y = [[0] * len(x) for _ in range(len(x))] for i in range(len(x)): y[i][i] = 1 while n > 0: if n & 1: y = mat_mul(x, y) x = mat_mul(x, x) n >>= 1 return y a, b = map(int, input().split()) n = int(input()) if isqrt(b) ** 2 == b: print((pow(a + isqrt(b), n, mod) + pow(a - isqrt(b), n, mod)) % mod) else: A = [[a, b], [1, a]] M = mat_pow(A, n) print(M[0][0] * 2 % mod)