#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<iomanip>
#include<cctype>
using namespace std;
#define ll long long
#define edl putchar('\n')
#define sscc ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define FORLL(i,a,b) for(ll i=a;i<=b;i++)
#define ROFLL(i,a,b) for(ll i=a;i>=b;i--)
#define mst(a) memset(a,0,ssizeof(a))
#define mstn(a,n) memset(a,n,ssizeof(a))
#define zero(x)(((x)>0?(x):-(x))<eps)
const int ssize=15;
int i,j;
int n,m,f[ssize],a[ssize];
struct Matrix {
ll a[ssize][ssize];
Matrix() {
memset(a,0,sizeof(a));
}
void init() {
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
a[i][j]=(i==j);
}
Matrix operator + (const Matrix &B)const {
Matrix C;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
C.a[i][j]=(a[i][j]+B.a[i][j])%m;
return C;
}
Matrix operator * (const Matrix &B)const {
Matrix C;
for(int i=0; i<n; i++)
for(int k=0; k<n; k++)
for(int j=0; j<n; j++)
C.a[i][j]=(C.a[i][j]%m+1LL*a[i][k]*B.a[k][j]%m+m)%m;
return C;
}
Matrix operator ^ (const ll &t)const {
Matrix A=(*this),res;
res.init();
ll p=t;
while(p) {
if(p&1)res=res*A;
A=A*A;
p>>=1;
}
return res;
}
};
int main() {
int k,i;
while(scanf("%d%d",&k,&m)!=EOF)
{
n=10;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
f[n]=0;
for(i=0;i<n;i++) //求前10项
f[i]=i;
for(i=0;i<n;i++)
f[n]+=a[i]*f[n-1-i];
if(k<=n)
printf("%d\n",f[k]);
else
{
Matrix A;
for(i=1;i<n;i++) //构造矩阵A
A.a[i][i-1]=1;
for(i=0;i<n;i++)
A.a[i][n-1]=a[n-1-i];
//A^(k-10)
Matrix res=A^(k-10);
Matrix origin;
for(i=0;i<n;i++) //前10项的矩阵[f(1),f(2),f(3)....f(10)]
origin.a[0][i]=f[i+1];
//[f(1),f(2),f(3)....f(10)] * A^(n-10)
Matrix ans;
ans=origin*res;
printf("%d\n",ans.a[0][n-1]%m);
}
}
}