#![allow(unused_imports)] use std::cmp::*; use std::collections::*; use std::io::Write; use std::ops::Bound::*; #[allow(unused_macros)] macro_rules! debug { ($($e:expr),*) => { #[cfg(debug_assertions)] $({ let (e, mut err) = (stringify!($e), std::io::stderr()); writeln!(err, "{} = {:?}", e, $e).unwrap() })* }; } fn main() { let v = read_vec::(); let (a, b) = (v[0], v[1]); let n = read::(); if n == 0 { println!("{}", 2); return; } if n == 1 { println!("{}", (2 * a).rem_euclid(1000_000_007)); return; } let mut c = mat_zeros(2, 2); c[0][0] = (2 * a).rem_euclid(1000_000_007); c[0][1] = (b - a * a).rem_euclid(1000_000_007); c[1][0] = 1; c[1][1] = 0; c = matpow(&c, n - 1); println!( "{}", ((c[0][0] * 2 * a) % 1000_000_007 + c[0][1] * 2) % (1000_000_007) ); } type mat = Vec>; fn mat_zeros(n: usize, m: usize) -> mat { vec![vec![0i64; m]; n] } fn matmul(a: &mat, b: &mat) -> mat { let mut c = mat_zeros(a.len(), b[0].len()); for i in 0..a.len() { for k in 0..b.len() { for j in 0..b[0].len() { c[i][j] += (a[i][k] * b[k][j]) % 1000_000_007; c[i][j] %= 1000_000_007; } } } c } fn matpow(a: &mat, n: u64) -> mat { let mut b = mat_zeros(a.len(), a.len()); for i in 0..a.len() { b[i][i] = 1; } let mut n = n; let mut a: mat = a.clone(); while n > 0 { if n & 1 == 1 { b = matmul(&a, &b); } a = matmul(&a, &a); n >>= 1; } b } fn read() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec() -> Vec { read::() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() }