#include template class array_matrix { public: array_matrix() { for (auto& v : table) { for (auto& e : v) { e = T{0}; } } } array_matrix(const array_matrix& m) : table{m.table} {} array_matrix& operator=(const array_matrix& m) { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { table[i][j] = m[i][j]; } } return *this; } const std::array& operator[](const int r) const { return table[r]; } std::array& operator[](const int r) { return table[r]; } friend array_matrix operator+(const array_matrix& m) { return m; } friend array_matrix operator-(const array_matrix& m) { array_matrix ans; for (int r = 0; r < row; r++) { for (int c = 0; c < column; c++) { ans.table[r][c] = -m.table[r][c]; } } return ans; } friend array_matrix operator+(const array_matrix& m1, const array_matrix& m2) { array_matrix ans; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { ans[i][j] = m1.table[i][j] + m2.table[i][j]; } } return ans; } friend array_matrix operator-(const array_matrix& m1, const array_matrix& m2) { array_matrix ans; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { ans[i][j] = m1.table[i][j] - m2.table[i][j]; } } return ans; } template friend array_matrix operator*(const array_matrix& m1, const array_matrix& m2) { array_matrix ans; for (int i = 0; i < row; i++) { for (int j = 0; j < c; j++) { for (int k = 0; k < column; k++) { ans[i][j] += m1.table[i][k] * m2.table[k][j]; } } } return ans; } friend array_matrix operator*(const array_matrix& m, const T& t) { array_matrix ans; for (int i = 0; i < m.row; i++) { for (int j = 0; j < m.column; j++) { ans[i][j] = m.table[i][j] * t; } } return ans; } friend array_matrix operator/(const array_matrix& m, const T& t) { array_matrix ans; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { ans[i][j] = m.table[i][j] / t; } } return ans; } friend array_matrix operator*(const T& t, const array_matrix& m) { return m * t; } friend array_matrix operator^(const array_matrix& m, const unsigned long long n) { return n == 0 ? array_matrix::I() : n % 2 == 1 ? m * (m ^ (n - 1)) : ((m * m) ^ (n / 2)); } friend array_matrix& operator+=(array_matrix& m1, const array_matrix& m2) { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { m1.table[i][j] += m2.table[i][j]; } } return m1; } friend array_matrix& operator-=(array_matrix& m1, const array_matrix& m2) { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { m1.table[i][j] -= m2.table[i][j]; } } return m1; } friend array_matrix& operator*=(array_matrix& m1, const array_matrix& m2) { return m1 = m1 * m2; } friend array_matrix& operator*=(array_matrix& m, const T& t) { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { m.table[i][j] *= t; } } return m; } friend array_matrix& operator/=(array_matrix& m, const T& t) { assert(t != 0); for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { m.table[i][j] /= t; } } return m; } friend array_matrix& operator^=(array_matrix& m, const unsigned long long n) { return m = m ^ n; } friend std::ostream& operator<<(std::ostream& os, const array_matrix& m) { os << "[\n"; for (int i = 0; i < row; i++) { os << "["; for (int j = 0; j < column; j++) { os << m[i][j] << ","; } os << "]\n"; } return (os << "]"); } static array_matrix I() { static_assert(row == column, "Must be rectangular!"); array_matrix ans; for (int i = 0; i < row; i++) { ans[i][i] = 1; } return ans; } private: std::array, row> table; }; using ll = long long; using uint = unsigned int; using ull = unsigned long long; using ld = long double; template using max_heap = std::priority_queue; template using min_heap = std::priority_queue, std::greater>; template constexpr std::pair extgcd(const T a, const T b) { if (b == 0) { return std::pair{1, 0}; } const auto g = std::gcd(a, b), da = std::abs(b) / g; const auto p = extgcd(b, a % b); const auto x = (da + p.second % da) % da, y = (g - a * x) / b; return {x, y}; } template constexpr T inverse(const T a, const T mod) { return extgcd(a, mod).first; } template class modint_base { public: template static std::enable_if_t mod() { return mod_ref(); } template static constexpr std::enable_if_t mod() { return mod_value; } template static void set_mod(const std::enable_if_t mod) { mod_ref() = mod, inv_ref() = {1, 1}; } modint_base() : v{0} {} modint_base(const ll val) : v{norm(static_cast(val % static_cast(mod()) + static_cast(mod())))} {} modint_base(const modint_base& n) : v{n()} {} explicit operator bool() const { return v != 0; } explicit operator int() const { return v; } explicit operator uint() const { return v; } explicit operator ll() const { return v; } explicit operator ull() const { return v; } bool operator!() const { return not static_cast(*this); } modint_base& operator=(const modint_base& m) { return v = m(), (*this); } modint_base& operator=(const ll val) { return v = norm(uint(val % static_cast(mod()) + static_cast(mod()))), (*this); } friend modint_base operator+(const modint_base& m) { return m; } friend modint_base operator-(const modint_base& m) { return make(norm(mod() - m.v)); } friend modint_base operator+(const modint_base& m1, const modint_base& m2) { return make(norm(m1.v + m2.v)); } friend modint_base operator-(const modint_base& m1, const modint_base& m2) { return make(norm(m1.v + mod() - m2.v)); } friend modint_base operator*(const modint_base& m1, const modint_base& m2) { return make(static_cast(static_cast(m1.v) * static_cast(m2.v) % static_cast(mod()))); } friend modint_base operator/(const modint_base& m1, const modint_base& m2) { return m1 * inv(m2.v); } friend modint_base operator+(const modint_base& m, const ll val) { return modint_base{static_cast(m.v) + val}; } friend modint_base operator-(const modint_base& m, const ll val) { return modint_base{static_cast(m.v) - val}; } friend modint_base operator*(const modint_base& m, const ll val) { return modint_base{static_cast(m.v) * (val % static_cast(mod()))}; } friend modint_base operator/(const modint_base& m, const ll val) { return modint_base{static_cast(m.v) * inv(val)}; } friend modint_base operator+(const ll val, const modint_base& m) { return modint_base{static_cast(m.v) + val}; } friend modint_base operator-(const ll val, const modint_base& m) { return modint_base{-static_cast(m.v) + val}; } friend modint_base operator*(const ll val, const modint_base& m) { return modint_base{static_cast(m.v) * (val % static_cast(mod()))}; } friend modint_base operator/(const ll val, const modint_base& m) { return modint_base{val * inv(static_cast(m.v))}; } friend modint_base& operator+=(modint_base& m1, const modint_base& m2) { return m1 = m1 + m2; } friend modint_base& operator-=(modint_base& m1, const modint_base& m2) { return m1 = m1 - m2; } friend modint_base& operator*=(modint_base& m1, const modint_base& m2) { return m1 = m1 * m2; } friend modint_base& operator/=(modint_base& m1, const modint_base& m2) { return m1 = m1 / m2; } friend modint_base& operator+=(modint_base& m, const ll val) { return m = m + val; } friend modint_base& operator-=(modint_base& m, const ll val) { return m = m - val; } friend modint_base& operator*=(modint_base& m, const ll val) { return m = m * val; } friend modint_base& operator/=(modint_base& m, const ll val) { return m = m / val; } friend modint_base operator^(const modint_base& m, const ll n) { return power(m.v, n); } friend modint_base& operator^=(modint_base& m, const ll n) { return m = m ^ n; } friend bool operator==(const modint_base& m1, const modint_base& m2) { return m1.v == m2.v; } friend bool operator!=(const modint_base& m1, const modint_base& m2) { return not(m1 == m2); } friend bool operator==(const modint_base& m, const ll val) { return m.v == norm(static_cast(static_cast(mod()) + val % static_cast(mod()))); } friend bool operator!=(const modint_base& m, const ll val) { return not(m == val); } friend bool operator==(const ll val, const modint_base& m) { return m.v == norm(static_cast(static_cast(mod()) + val % static_cast(mod()))); } friend bool operator!=(const ll val, const modint_base& m) { return not(m == val); } friend std::istream& operator>>(std::istream& is, modint_base& m) { ll v; return is >> v, m = v, is; } friend std::ostream& operator<<(std::ostream& os, const modint_base& m) { return os << m(); } uint operator()() const { return v; } static modint_base fact(const int n) { auto& fact_ = fact_ref(); if (n < (int)fact_.size()) { return fact_[n]; } for (int i = (int)fact_.size(); i <= n; i++) { fact_.push_back(fact_.back() * i); } return fact_.back(); } static modint_base inv_fact(const int n) { auto& inv_fact_ = inv_fact_ref(); if (n < (int)inv_fact_.size()) { return inv_fact_[n]; } for (int i = inv_fact_.size(); i <= n; i++) { inv_fact_.push_back(inv_fact_.back() * modint_base::small_inv(i)); } return inv_fact_.back(); } static modint_base perm(const int n, const int k) { return k > n ? modint_base{0} : fact(n) * inv_fact(n - k); } static modint_base comb(const int n, const int k) { return k > n ? modint_base{0} : fact(n) * inv_fact(n - k) * inv_fact(k); } std::pair quad() const { const auto ans = quad_r(v, mod()); ll x = std::get<0>(ans), y = std::get<1>(ans); if (y < 0) { x = -x, y = -y; } return {x, y}; } private: static std::tuple quad_r(const ll r, const ll p) // r = x/y (mod p), (x,y,z) s.t. x=yr+pz { if (std::abs(r) <= 1000) { return {r, 1, 0}; } ll nr = p % r, q = p / r; if (nr * 2LL >= r) { nr -= r, q++; } if (nr * 2LL <= -r) { nr += r, q--; } const auto sub = quad_r(nr, r); const ll x = std::get<0>(sub), z = std::get<1>(sub), y = std::get<2>(sub); return {x, y - q * z, z}; } template static std::enable_if_t mod_ref() { static UInt mod = 0; return mod; } static uint norm(const uint x) { return x < mod() ? x : x - mod(); } static modint_base make(const uint x) { modint_base m; return m.v = x, m; } static modint_base power(modint_base x, ull n) { modint_base ans = 1; for (; n; n >>= 1, x *= x) { if (n & 1) { ans *= x; } } return ans; } static modint_base small_inv(const int n) { auto& inv_ = inv_ref(); if (n < (int)inv_.size()) { return inv_[n]; } for (int i = inv_.size(); i <= n; i++) { inv_.push_back(-inv_[modint_base::mod() % i] * (modint_base::mod() / i)); } return inv_.back(); } static modint_base inv(const ll v) { return v <= 2000000 ? small_inv(static_cast(v)) : modint_base{inverse(v, static_cast(mod()))}; } static std::vector& inv_ref() { static std::vector inv_{1, 1}; return inv_; } static std::vector& fact_ref() { static std::vector fact_{1, 1}; return fact_; } static std::vector& inv_fact_ref() { static std::vector inv_fact_{1, 1}; return inv_fact_; } uint v; }; template using modint = modint_base; template using dynamic_modint = modint_base; constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; } constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; } constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); } constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; } constexpr ull ceil2(const ull v) { return 1ULL << clog(v); } constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; } constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; } template void bset(T& mask, const int ind) { mask |= ((T)1 << ind); } template void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); } template void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); } template void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); } template bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } template constexpr T inf_v = std::numeric_limits::max() / 4; template constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; template constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN(n - 1) * T{10}; } template struct fix : F { fix(F&& f) : F{std::forward(f)} {} template auto operator()(Args&&... args) const { return F::operator()(*this, std::forward(args)...); } }; template auto nd_array(int const (&szs)[n], const T x = T{}) { if constexpr (i == n) { return x; } else { return std::vector(szs[i], nd_array(szs, x)); } } class printer { public: printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); } template int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; } template int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; } template int fmt(const std::string& s, const Args&... args) { return rec(s, 0, args...); } private: template int dump(const T& v) { return m_os << v, 0; } template int dump(const std::vector& vs) { for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); } return 0; } template int dump(const std::vector>& vss) { for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); } return 0; } template int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; } int rec(const std::string& s, int index) { return m_os << s.substr(index, s.size()), 0; } template int rec(const std::string& s, int index, const T& v, const Args&... args) { return index == s.size() ? 0 : s[index] == '%' ? (tdump(v), rec(s, index + 1, args...)) : (m_os << s[index], rec(s, index + 1, v, args...)); } std::ostream& m_os; }; printer out; class scanner { public: scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); } template T val() { T v; return m_is >> v, v; } template T val(const T offset) { return val() - offset; } template std::vector vec(const int n) { return make_v(n, [this]() { return val(); }); } template std::vector vec(const int n, const T offset) { return make_v(n, [this, offset]() { return val(offset); }); } template std::vector> vvec(const int n0, const int n1) { return make_v(n0, [this, n1]() { return vec(n1); }); } template std::vector> vvec(const int n0, const int n1, const T offset) { return make_v(n0, [this, n1, offset]() { return vec(n1, offset); }); } template auto tup() { return std::tuple...>{val()...}; } template auto tup(const Args&... offsets) { return std::tuple...>{val(offsets)...}; } private: template std::vector make_v(const int n, F f) { std::vector ans; for (int i = 0; i < n; i++) { ans.push_back(f()); } return ans; } std::istream& m_is; }; scanner in; # define SHOW(...) static_cast(0) int main() { using mint = modint<1000000007>; const auto [a, b] = in.tup(); const auto N = in.val(); array_matrix mat; mat[0][0] = 2 * a; mat[0][1] = b - a * a; mat[1][0] = 1; mat[1][1] = 0; mat = mat ^ N; const mint ans = 2 * a * mat[1][0] + 2 * mat[1][1]; out.ln(ans); return 0; }