#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
    using namespace std;
    #define pii pair<int,int>
    #define fir first
    #define sec second
    #define MP make_pair
    #define rep(i,x,y) for (int i=(x);i<=(y);i++)
    #define drep(i,x,y) for (int i=(x);i>=(y);i--)
    #define go(x) for (int i=head[x];i;i=edge[i].nxt)
    #define templ template<typename T>
    #define sz 666
    typedef long long ll;
    typedef double db;
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
    templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
    templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
    templ inline void read(T& t)
    {
        t=0;char f=0,ch=getchar();double d=0.1;
        while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
        while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
        if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
        t=(f?-t:t);
    }
    template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
    char __sr[1<<21],__z[20];int __C=-1,__zz=0;
    inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
    inline void print(register int x)
    {
        if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
        while(__z[++__zz]=x%10+48,x/=10);
        while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
    }
    void file()
    {
        #ifdef NTFOrz
        freopen("a.in","r",stdin);
        #endif
    }
    inline void chktime()
    {
        #ifndef ONLINE_JUDGE
        cout<<(clock()-t)/1000.0<<'\n';
        #endif
    }
    #ifdef mod
    ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
    ll inv(ll x){return ksm(x,mod-2);}
    #else
    ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
    #endif
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;

ll mod;
int n;
struct hh
{
	int l,r;
	const bool operator < (const hh &x) const {return l==x.l?r<x.r:l<x.l;}
}a[sz];

ll dp[sz][sz]; // 前i个，选了j个l，方案数 

int Sqrt1(int x){int ret=0;while ((ret+1)*(ret+1)<=x) ++ret;return ret;}
int Sqrt2(int x){int ret=0;while (ret*ret<x) ++ret;return ret-1;}

int main()
{
    file();
	read(n,mod);
	rep(i,0,n*2-1) 
		if (i<n) a[i+1].l=Sqrt2(n*n-i*i),a[i+1].r=min(n+n-1,Sqrt1(4*n*n-i*i));
		else a[i+1].l=Sqrt1(4*n*n-i*i),a[i+1].r=-1;
	sort(a+1,a+n+n+1);
//	rep(i,1,n+n) printf("%d %d\n",a[i].l,a[i].r);
	ll ans=0;int cc=0;
	rep(i,1,n+n) cc+=(a[i].r!=-1);
	rep(k,0,cc)
	{
		rep(i,0,n+n) rep(j,0,n+n) dp[i][j]=0;
		dp[0][0]=1;
		for (int i=1,p=0,q=cc,r=-1;p+=(a[i].r!=-1),q-=(a[i].r!=-1),r+=(a[i].r==-1),i<=n+n;i++)
		// p:前i个里面l的个数
		// q:后面l的个数
		// r:i-1前面普通的个数 
		rep(j,0,k)
		{
			// j：前i个里面选了l的个数
			// cnt[i-1]-j/(j-1)：前i-1个里面没选l的个数，也就是选了r的个数
			// cnt[n+n]-k-(cnt[i]-j)：后面的数里面选了r的个数 
			if (a[i].r==-1&&(a[i].l-(j+r)+1)>0) 
				dp[i][j]=dp[i-1][j]*(a[i].l-(j+r)+1)%mod;
			else
			{
				int cur;
				cur=(a[i].l-(j+r)+1);
				if (j&&cur>0) dp[i][j]=dp[i-1][j-1]*cur%mod;
				cur=(a[i].r-((n+n-cc)+k+p-j-1)+1);
				if (cur>0) (dp[i][j]+=dp[i-1][j]*cur%mod)%=mod;
			}
		}
//		if (dp[n+n][k]==225543327) continue;
		if (k&1) ans=(ans-dp[n+n][k]+mod)%mod;
		else ans=(ans+dp[n+n][k])%mod;	
//		printf("%lld\n",dp[n+n][k]);
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 4352