A, B, C, D = map(int, input().split()) N = int(input()) M = 10**9+7 p = (N-1) % 8 n = (N-1) // 8 V = None if p == 0: V = 2 * B elif p == 1: V = 2 * (B - C) elif p == 2: V = -4 * C elif p == 3: V = -4 * (A + C) elif p == 4: V = -8 * A elif p == 5: V = -8 * (A - D) elif p == 6: V = 16 * D elif p == 7: V = 16 * (B - C) V = (V + M) % M V = (V * pow(16, n, M)) % M print(V)