#include #include using namespace std; namespace my{ using ml=atcoder::modint1000000007; auto&operator>>(istream&i,ml&x){int t;i>>t;x=t;return i;} auto&operator<<(ostream&o,const ml&x){return o<<(int)x.val();} #define done(...) return pp(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define FO(n) for(ll ij=n;ij-->0;) #define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);isync_with_stdio(0);cout<>(istream&i,ulll&x){ull t;i>>t;x=t;return i;} ostream&operator<<(ostream&o,const ulll&x){return(x<10?o:o<>(istream&i,lll&x){ll t;i>>t;x=t;return i;} ostream&operator<<(ostream&o,const lll&x){return o<0?x:-x);} constexpr auto range(bool s,auto...a){arrayr{0,0,1};ll I=0;((r[I++]=a),...);if(!s&&I==1)swap(r[0],r[1]);r[0]-=s;if(s)r[2]*=-1;return r;} constexpr char newline=10; constexpr char space=32; lll pow(lll x,ll n){assert(n>=0);lll r=1;while(n)n&1?r*=x:r,x*=x,n>>=1;return r;} templateauto pack_kth(const auto&...a){return get(make_tuple(a...));} templateauto pack_slice_impl(index_sequence, const auto&...a){return array{get(forward_as_tuple(a...))...};} templateauto pack_slice(const auto&...a){return pack_slice_impl(make_index_sequence{},a...);} templateconcept vectorial=is_base_of_v,V>; templateistream&operator>>(istream&i,vector&v){fe(v,e)i>>e;return i;} templateostream&operator<<(ostream&o,const vector&v){fe(v,e)o<?newline:space);return o;} templatestruct vec:vector{ using vector::vector; vec(const vector&v){vector::operator=(v);} templaterequires(sizeof...(A)>=3)vec(A...a){const ll n=sizeof...(a)-1;auto t=pack_slice(a...);ll s[n];fo(i,n)s[i]=t[i];*this=make_vec(s,pack_kth(a...));} templatestatic auto make_vec(const ll(&s)[n],T x){if constexpr(i==n-1)return vec(s[i],x);else{auto X=make_vec(s,x);return vec(s[i],X);}} vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;} vec operator^(const vec&u)const{return vec{*this}^=u;} vec&operator+=(const vec&u){vec&v=*this;fo(i,v.size())v[i]+=u[i];return v;} vec&operator-=(const vec&u){vec&v=*this;fo(i,v.size())v[i]-=u[i];return v;} vec operator+(const vec&u)const{return vec{*this}+=u;} vec operator-(const vec&u)const{return vec{*this}-=u;} vec&operator++(){fe(*this,e)++e;return*this;} vec&operator--(){fe(*this,e)--e;return*this;} vec operator-()const{vec v=*this;fe(v,e)e=-e;return v;} ll size()const{return vector::size();} }; templatestruct tensor_helper{using type=vec::type>;}; templatestruct tensor_helper<0,T>{using type=T;}; templateusing tensor=typename tensor_helper::type; templaterequires(sizeof...(A)>=2)vec(A...a)->vec(declval>()))>>>; vec(ll)->vec; void lin(auto&...a){(cin>>...>>a);} templatevoid pp(const auto&...a){ll n=sizeof...(a);((cout<0,c)),...);cout<struct matrix:vec>{ using vec>::vec; matrix()=default; matrix(ll h){this->resize(h,vec(h));} matrix(ll h,ll w,T x={}){this->resize(h,vec(w,x));} inline ll h()const{return this->size();} inline ll w()const{return this->size()?(*this)[0].size():0;} auto operator*(const matrix&a)const{return matrix{*this}*=a;} auto&operator*=(const matrix&a){ assert(w()==a.h()); matrix r(h(),a.w()); fo(i,h())fo(k,w())fo(j,a.w())r[i][j]+=(*this)[i][k]*a[k][j]; swap(*this,r); return*this; } friend vecoperator*(const vec&v,const matrix&a){ assert(v.size()==a.h()); vecr(a.w()); fo(i,a.h())fo(j,a.w())r[i]+=v[i]*a[i][j]; return r; } auto pow(ll n)const{ matrix x{*this}; matrix r(h()); fo(i,h())r[i][i]=1; while(n)n&1?r*=x:r,x*=x,n>>=1; return r; } T det()const{ assert(h()==w()); matrix a{*this}; bool is_neg=0; fo(I,h()){ if(a[I][I]==0){ fo(i,I+1,h()){ if(a[i][I]!=0){ swap(a[i],a[I]); is_neg^=1; break; } } if(a[I][I]==0)return 0; } fo(i,I+1,h()){ while(a[i][I]!=0){ ll q=a[I][I].val()/a[i][I].val(); T minus_q{-q}; fo(j,I,w())a[I][j]+=a[i][j]*minus_q; swap(a[I],a[i]); is_neg^=1; } } } T r=1; fo(I,h())r*=a[I][I]; return r*(1-is_neg*2); } auto inner_gauss_jordan_elimination()const{ matrix r{*this}; ll I=0; T det_prime_mod=1; fo(J,w()){ if(I==h())break; if(r[I][J]==0){ fo(i,I+1,h()){ if(r[i][J]!=0){ swap(r[i],r[I]); det_prime_mod=-det_prime_mod; break; } } if(r[I][J]==0){ det_prime_mod=0; continue; } } det_prime_mod*=r[I][J]; T C=r[I][J].inv(); fo(j,J,w())r[I][j]*=C; fo(i,h())if(T c=r[i][J];i!=I)fo(j,J,w())r[i][j]-=r[I][j]*c; ++I; } return tuple{r,I,det_prime_mod}; } auto gauss_jordan_elimination()const{return get<0>(inner_gauss_jordan_elimination());} auto inv()const{ assert(h()==w()); assert(det()!=0); matrix a(h(),w()*2); fo(i,h())fo(j,w())a[i][j]=(*this)[i][j]; fo(i,h())a[i][i+w()]=1; a=a.gauss_jordan_elimination(); matrix r(h(),w()); fo(i,h())fo(j,w())r[i][j]=a[i][j+w()]; return r; } }; single_testcase void solve(){ LL(a,b,N); if(N==0)done(0); matrixA{{a,b},{1,0}}; A=A.pow(N-1); pp(A[0][0]); }}