#include #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; ll a, b, n, MOD = 1e9 + 7; map mp; ll mod_pow(ll x, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll solve(ll n) { if (mp.find(n) != mp.end()) return mp[n]; if (n == 0) { mp[0] = 2; return mp[0]; } if (n == 1) { mp[1] = (2 * a) % MOD; return mp[1]; } if (n % 2) { mp[n] = (solve(n / 2 + 1) * solve(n / 2) - ((mod_pow(a * a - b, n / 2, MOD) * 2 * a) % MOD) + MOD) % MOD; } else { mp[n] = (solve(n / 2) * solve(n / 2) - ((mod_pow(a * a - b, n / 2, MOD) * 2) % MOD) + MOD) % MOD; } return mp[n]; } int main() { cin >> a >> b >> n; if (a == 0) { if (b == 0) { cout << "0\n"; return 0; } else { if (n % 2) { cout << "0\n"; return 0; } else { cout << (mod_pow(b, n / 2, MOD) * 2) % MOD << "\n"; return 0; } } } if (b == 0) { cout << (mod_pow(a, n, MOD) * 2) % MOD << "\n"; return 0; } cout << solve(n) << "\n"; }