#include <cstdio>
#include <iostream>
using namespace std;
int mod;
typedef struct
{
long long m[10][10];
}matrix;
matrix I={1,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,
0,0,0,0,0,1,0,0,0,0,
0,0,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,0,1,};
matrix mul(matrix a,matrix b)
{
int i,j,k;
matrix c;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
c.m[i][j]=0;
for(k=0;k<10;k++)
c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
c.m[i][j]%=mod;
}
return c;
}
matrix quick_mod(int n,int a0,int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9)
{matrix P={a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,
1,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,
0,0,0,0,0,1,0,0,0,0,
0,0,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,0,1,0,};
matrix a=P,b=I;
while(n>=1)
{
if(n&1)
b=mul(a,b);
n=n>>1;
a=mul(a,a);
}
return b;
}
int main()
{
int a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,k;
while(scanf("%d%d",&k,&mod)!=-1)
{
scanf("%d%d%d%d%d%d%d%d%d%d",&a0,&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8,&a9);
matrix temp;
int ans;
if(k<10)
ans=k%mod;
else
{
temp=quick_mod(k-9,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9);
ans=((temp.m[0][0]*9)%mod+(temp.m[0][1]*8)%mod+(temp.m[0][2]*7)%mod+(temp.m[0][3]*6)%mod+(temp.m[0][4]*5)%mod+(temp.m[0][5]*4)%mod+(temp.m[0][6]*3)%mod+(temp.m[0][7]*2)%mod+(temp.m[0][8])%mod)%mod;
}
printf("%d\n",ans);
}
return 0;
}