mod = 10 ** 9 + 7 a, b, c, d = map(int, input().split()) n = int(input()) a *= pow(16, n // 8, mod) b *= pow(16, n // 8, mod) c *= pow(16, n // 8, mod) d *= pow(16, n // 8, mod) if n % 8 == 0: print((b + d) % mod) elif n % 8 == 1: print((2 * b) % mod) elif n % 8 == 2: print((2 * b - 2 * c) % mod) elif n % 8 == 3: print((-4 * c) % mod) elif n % 8 == 4: print((-4 * (a + c)) % mod) elif n % 8 == 5: print((-8 * a) % mod) elif n % 8 == 6: print((8 * d - 8 * a) % mod) elif n % 8 == 7: print((16 * d) % mod)