#include using namespace std; using LL=long long; using ULL=unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) const ULL M = 1000000007; const int vector_dim = 3; struct Vec{ ULL X[vector_dim]={}; ULL& operator[](int x){ return X[x]; } const ULL& operator[](int x)const{ return X[x]; } }; Vec operator+(const Vec& l,const Vec& r){ Vec res; rep(i,vector_dim){ ULL x = l[i]+r[i]; if(x>=M) x-=M; res[i]=x; } return res; } Vec operator-(const Vec& l,const Vec& r){ Vec res; rep(i,vector_dim){ ULL x = l[i]+M-r[i]; if(x>=M) x-=M; res[i]=x; } return res; } ULL operator*(const Vec& l,const Vec& r){ ULL res = 0; rep(i,vector_dim) res+=l[i]*r[i]; res%=M; return res; } struct Matrix{ ULL X[vector_dim][vector_dim]={}; ULL* operator[](int x){ return X[x]; } const ULL* operator[](int x)const{ return X[x]; } static Matrix id(){ Matrix res; rep(i,vector_dim) res[i][i]=1; return res;} Matrix pow(ULL idx)const; }; Matrix operator+(const Matrix& l,const Matrix& r){ Matrix res; rep(i,vector_dim) rep(j,vector_dim){ ULL x = l[i][j]+r[i][j]; if(x>=M) x-=M; res[i][j]=x; } return res; } Matrix operator-(const Matrix& l,const Matrix& r){ Matrix res; rep(i,vector_dim) rep(j,vector_dim){ ULL x = l[i][j]+M+r[i][j]; if(x>=M) x-=M; res[i][j]=x; } return res; } Matrix operator*(const Matrix& l,const Matrix& r){ Matrix res; rep(i,vector_dim) rep(j,vector_dim){ rep(k,vector_dim) res[i][j] += l[i][k] * r[k][j]; res[i][j] %= M; } return res; } Matrix Matrix::pow(ULL idx)const{ if(idx==0) return id(); Matrix res = pow(idx/2); res=res*res; if(idx%2==1) res=res*(*this); return res; } Vec operator*(const Vec& l,const Matrix& r){ Vec res; rep(i,vector_dim){ rep(j,vector_dim) res[i] += l[j] * r[j][i]; res[i] %= M; } return res; } int N; int main(){ cin>>N; Matrix G = {{{1,0,M-1},{M-1,1,0},{0,M-1,1}}}; G = G.pow(N-1); Vec A; rep(i,3) cin>>A[i]; A = A*G; cout<