#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
int k,x;
unsigned long long answer=1;
int q[100],w[100],ji=0;
int main()
{
freopen ("equationz.in","r",stdin);
freopen ("equationz.out","w",stdout);
cin>>k>>x;
int e;
e=x;
for (int i=1;i<e;i++)
{
x=(x*e)%1000;
}
int c,d;
c=x-1;
d=k-1;
for (;d>=1;d--)
{
ji++;
q[ji]=c;
w[ji]=d;
c--;
}
for (int i=1;i<=ji;i++)
{
for (int j=1;j<=ji;j++)
{
int a,b;
a=q[i];
b=w[j];
while(b^=a^=b^=a%=b);
q[i]/=a;
w[j]/=a;
}
}
for (int i=1;i<=ji;i++)
{
answer*=q[i];
}
cout<<answer;
return 0;
}