#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
long long X,K;
double S=1;
long long pl(long long num,long long level,long long moder)
{
long long temp=1;
while (level)
{
temp=(temp*num)%moder;
level--;
}
return(temp);
}
long long Up[10000];
long long Down[10000];
int main()
{
freopen("equation.in","r",stdin);
freopen("equation.out","w",stdout);
cin>>K>>X;
X=X%1000;
long long B=pl(X,X,1000);
if(B==0)
{
cout<<0<<endl;
return 0;
}
int ups=1,downs=1;
int top=0;
for (int i=1;i<=K-1;i++)
{
Up[ups]=B-ups;
ups++;
}
for (int i=K-1;i>=1;i--)
{
Down[downs]=i;
downs++;
}
top=downs-1;
/*
for (int i=1;i<=top;i++)
cout<<Up[i]<<" ";
cout<<endl;
for (int i=1;i<=top;i++)
cout<<Down[i]<<" ";
cout<<endl;
*/
int p1=1;
int p2=top;
while(p1<=top)
{
S=S*Up[p2]/Down[p1];
p2--;
p1++;
}
if(S>1)
printf("%0.lf\n",S);
else
cout<<0<<endl;
return 0;
}