//package Main;
import java.util.Scanner;
public class Main
{
static int [][] mat=new int [2][2];
static int [][] unit=new int [2][2];
static int [][] as=new int [2][2];
//static Solve ans;
static int a,b,n;
static int mod=7;
public static void main(String[] args)
{
Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{
a=cin.nextInt();
b=cin.nextInt();
n=cin.nextInt();
if(a==0&&b==0&&n==0)break;
mat[0][0]=a;
mat[0][1]=1;
mat[1][0]=b;
mat[1][1]=0;
unit[0][0]=unit[0][1]=1;
unit[1][0]=unit[1][1]=0;
if(n<3)System.out.print(1);
else
{
as=Pow(n-2);
System.out.print(as[0][0]);
}
System.out.println();
}
}
static int[][] Pow(int n)
{
while(n>0)
{
if((n&1)>0)
unit=mul(unit,mat);
n>>=1;
mat=mul(mat, mat);
}
return unit;
}
static int[][] mul(int [][] a,int [][] b)
{
int i,j,k;
int [][] c=new int[2][2];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]+=a[i][k]*b[k][j];
c[i][j]%=mod;
}
}
return c;
}
}
-
-
-
-
-