#include #include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double #define INF 1000000000000000000 typedef pair pll; typedef pair pint; template struct Mat_cul { using Matrix = vector>; Matrix mat_add(Matrix A, Matrix B, const long long MOD) { int m = A.size(); int n = B[0].size(); Matrix C(m, vector(n)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { C[i][j] = A[i][j] + B[i][j]; C[i][j] %= MOD; } } return C; } Matrix mat_sub(Matrix A, Matrix B, const long long MOD) { int m = A.size(); int n = B[0].size(); Matrix C(m, vector(n)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { C[i][j] = A[i][j] - B[i][j] + MOD; C[i][j] %= MOD; } } return C; } Matrix mat_mul(Matrix A, Matrix B, const long long MOD) { int m = A.size(); int l = B.size(); int n = B[0].size(); Matrix C(m, vector(n)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < l; k++) { C[i][j] += A[i][k] * B[k][j]; C[i][j] %= MOD; } } } return C; } Matrix mat_pow_mod(Matrix A, ll p, const long long MOD) { Matrix ret(A.size(), vector(A.size(), 0)); for (int i = 0; i < A.size(); i++) ret[i][i] = 1; while (p > 0) { if (p & 1) ret = mat_mul(ret, A, MOD); A = mat_mul(A, A, MOD); p >>= 1; } return ret; } }; const ll MOD = pow(10, 9) + 7; int main() { cin.tie(0); ios::sync_with_stdio(false); Mat_cul M; ll a, b, n; cin >> a >> b >> n; if (n == 0) { cout << 0 << endl; return 0; } else if (n == 1) { cout << 1 << endl; return 0; } vector> A(2, vector(2)), B(2, vector(1)); A[0][0] = a, A[1][0] = b, A[0][1] = 1, A[1][1] = 0; B[0][0] = 1, B[1][0] = 0; vector> C = M.mat_pow_mod(A, n - 1, MOD), D = M.mat_mul(C, B, MOD); cout << D[0][0] << endl; }