stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define ALL(v) v.begin(), v.end()

typedef long long ll;



#include <bits/stdc++.h>

using namespace std;



ll floor_sum(ll n,ll m,ll a,ll b){

  ll ans=0;

  ans+=(n-1)*n*(a/m)/2+n*(b/m);

  a%=m;

  b%=m;



  ll y=(a*n+b)/m;

  if(y==0) return ans;

  ll x=(m*y-b+a-1)/a;

  ans+=(n-x)*y+floor_sum(y,a,m,a*x-m*y+b);

  return ans;

}



int main(){

  int t;

  cin>>t;

  

  rep(i,t){

    int n,m,a,b;

    cin>>n>>m>>a>>b;

  

    cout<<floor_sum(n,m,a,b)<<endl;

  }

  

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.151311529779782x):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define ALL(v) v.begin(), v.end()

typedef long long ll;



#include <stdio.h>

using namespace std;



ll floor_sum(ll n,ll m,ll a,ll b){

  ll ans=0;

  if(a>=m){

    ans+=(n-1)*n*(a/m)/2;

    a%=m;

  }

  if(b>=m){

    ans+=n*(b/m);

    b%=m;

  }



  ll y_max=(a*n+b)/m;

  if(y_max==0) return ans;

  ans+=floor_sum(y_max,a,m,a*n-y_max*m+b);

  return ans;

}



int main(){

  int t;

  scanf("%d",&t);

  

  while(t--){

    int n,m,a,b;

    scanf("%d%d%d%d",&n,&m,&a,&b);

    printf("%lld\n",floor_sum(n,m,a,b));

  }

  

  return 0;

}
// Slower C++ solution:
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define ALL(v) v.begin(), v.end()

typedef long long ll;



#include <bits/stdc++.h>

using namespace std;



ll floor_sum(ll n,ll m,ll a,ll b){

  ll ans=0;

  if(a>=m){

    ans+=(n-1)*n*(a/m)/2;

    a%=m;

  }

  if(b>=m){

    ans+=n*(b/m);

    b%=m;

  }



  ll y=(a*n+b)/m;

  if(y==0) return ans;

  ll x=(m*y-b+a-1)/a;

  ans+=(n-x)*y+floor_sum(y,a,m,a*x-m*y+b);

  return ans;

}



int main(){

  int t;

  cin>>t;

  

  rep(i,t){

    int n,m,a,b;

    cin>>n>>m>>a>>b;

  

    cout<<floor_sum(n,m,a,b)<<endl;

  }

  

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.149970295713632x):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define ALL(v) v.begin(), v.end()

typedef long long ll;



#include <stdio.h>

using namespace std;



ll floor_sum(ll n,ll m,ll a,ll b){

  ll ans=0;

  if(a>=m){

    ans+=(n-1)*n*(a/m)/2;

    a%=m;

  }

  if(b>=m){

    ans+=n*(b/m);

    b%=m;

  }



  ll y_max=(a*n+b)/m;

  if(y_max==0) return ans;

  ans+=floor_sum(y_max,a,m,a*n-y_max*m+b);

  return ans;

}



int main(){

  int t;

  scanf("%d",&t);

  

  while(t--){

    int n,m,a,b;

    scanf("%d%d%d%d",&n,&m,&a,&b);

    printf("%lld\n",floor_sum(n,m,a,b));

  }

  

  return 0;

}
// Slower C++ solution:
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define ALL(v) v.begin(), v.end()

typedef long long ll;



#include <bits/stdc++.h>

using namespace std;



ll floor_sum(ll n,ll m,ll a,ll b){

  ll ans=0;

  if(a>=m){

    ans+=(n-1)*n*(a/m)/2;

    a%=m;

  }

  if(b>=m){

    ans+=n*(b/m);

    b%=m;

  }



  ll y_max=(a*n+b)/m;

  if(y_max==0) return ans;

  ans+=floor_sum(y_max,a,m,a*n-y_max*m+b);

  return ans;

}



int main(){

  int t;

  cin>>t;

  

  rep(i,t){

    int n,m,a,b;

    cin>>n>>m>>a>>b;

  

    cout<<floor_sum(n,m,a,b)<<endl;

  }

  

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.150114647103148x):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define ALL(v) v.begin(), v.end()

typedef long long ll;



#include <stdio.h>

using namespace std;



ll floor_sum(ll n,ll m,ll a,ll b){

  ll ans=0;

  if(a>=m){

    ans+=(n-1)*n*(a/m)/2;

    a%=m;

  }

  if(b>=m){

    ans+=n*(b/m);

    b%=m;

  }



  ll y_max=(a*n+b)/m;

  if(y_max==0) return ans;

  ans+=floor_sum(y_max,a,m,a*n-y_max*m+b);

  return ans;

}



int main(){

  int t;

  scanf("%d",&t);

  

  while(t--){

    int n,m,a,b;

    scanf("%d%d%d%d",&n,&m,&a,&b);

    printf("%lld\n",floor_sum(n,m,a,b));

  }

  

  return 0;

}
// Slower C++ solution:
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define ALL(v) v.begin(), v.end()

typedef long long ll;



#include <bits/stdc++.h>

using namespace std;



ll floor_sum(ll n,ll m,ll a,ll b){

  ll ans=0;

  if(a>=m){

    ans+=(n-1)*n*(a/m)/2;

    a%=m;

  }

  if(b>=m){

    ans+=n*(b/m);

    b%=m;

  }



  ll y_max=(a*n+b)/m;

  if(y_max==0) return ans;

  ans+=floor_sum(y_max,a,m,a*n-y_max*m+b);

  return ans;

}



int main(){

  int t;

  cin>>t;

  

  rep(i,t){

    int n,m,a,b;

    cin>>n>>m>>a>>b;

  

    cout<<floor_sum(n,m,a,b)<<'\n';

  }

  

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.148144720510039x):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define ALL(v) v.begin(), v.end()

typedef long long ll;



#include <stdio.h>

using namespace std;



ll floor_sum(ll n,ll m,ll a,ll b){

  ll ans=0;

  if(a>=m){

    ans+=(n-1)*n*(a/m)/2;

    a%=m;

  }

  if(b>=m){

    ans+=n*(b/m);

    b%=m;

  }



  ll y_max=(a*n+b)/m;

  if(y_max==0) return ans;

  ans+=floor_sum(y_max,a,m,a*n-y_max*m+b);

  return ans;

}



int main(){

  int t;

  scanf("%d",&t);

  

  while(t--){

    int n,m,a,b;

    scanf("%d%d%d%d",&n,&m,&a,&b);

    printf("%lld\n",floor_sum(n,m,a,b));

  }

  

  return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cmath>

using namespace std;

const int N=510;

struct dat{

    int x,y,id;

    dat(){}

    dat(int x,int y,int id):

        x(x),y(y),id(id){}

}a[N];

int n,mod,cnt=0;

long long ans=0ll;

long long f[N][N];

inline bool cmp(dat x,dat y)

{

    if(x.x^y.x) return x.x<y.x;

    else return x.y<y.y;

}

inline long long add(long long x,long long y)

{

    return x+y>=mod?x+y-mod:x+y;

}

inline long long sub(long long x,long long y)

{

    return x-y<0?x-y+mod:x-y;

}

int main()

{

//    freopen("1.in","r",stdin);

    scanf("%d%d",&n,&mod);

    for(int k=0;k<2*n;k++)

    {

        int l=max(0,(int)ceil(sqrt(n*n-k*k))),r=min(2*n-1,(int)sqrt(n*n*4-k*k));

        if(k<n) a[++cnt]=dat(l-1,r,k);

        else a[++cnt]=dat(r,0,k);

    }

    sort(a+1,a+1+cnt,cmp);

    for(int k=0;k<=n;k++)

    {

        memset(f,0,sizeof f);

        f[0][0]=1ll;

        int x=0;

        for(int i=1;i<=cnt;i++)

        {

            for(int j=0;j<=k;j++)

            {

                if(a[i].id<n)

                {

                    if(a[i].x+1-x-(j-1)>0&&j)

                        f[i][j]=add(f[i][j],f[i-1][j-1]*(a[i].x+1-x-(j-1))%mod);

                    if(a[i].y+1-k-n-(i-1-x-j)>0)

                        f[i][j]=add(f[i][j],f[i-1][j]*(a[i].y+1-k-n-(i-1-x-j))%mod);

                }

                else

                {

                    if(a[i].x+1-x-j>0)

                        f[i][j]=f[i-1][j]*(a[i].x+1-x-j)%mod;

                }

            }

            if(a[i].id>=n) x++;

        }

        if(k&1) ans=sub(ans,f[cnt][k]);

        else ans=add(ans,f[cnt][k]);

    }

    printf("%lld\n",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 8.3501961159867x):
#include<bits/stdc++.h>

#define maxn 605

#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)

#define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--)

#define LL long long

using namespace std;



int n;

int mod;

int f[2][maxn];

int up[maxn],dn[maxn],d[maxn],c[maxn];

bool cmp(const int &u,const int &v){ return d[u] < d[v]; }



int main(){

	scanf("%d%d",&n,&mod);

	rep(i,0,2*n-1) up[i] = min((int)floor(sqrt(4 * n * n - i * i)),2*n-1) , dn[i] = (i <= n ? (int)ceil(sqrt(n * n - i * i)) : 0);

	rep(i,0,n-1) d[i] = dn[i] - 1;

	rep(i,n,2*n-1) d[i] = up[i];

	rep(i,0,2*n-1) c[i] = 2*n-i-1;

	stable_sort(c,c+2*n,cmp);int ans =0 ;

	rep(k,0,n){

		memset(f,0,sizeof f);

		int now = 1, pre = 0 , cntr = 0;

		f[now][0] = 1;

		rep(i,0,2*n-1){

			int u = c[i];

			swap(now,pre);

			rep(j,0,k) if(f[pre][j]){

				if(u >= n){	

					f[now][j] = (f[now][j] + 1ll * f[pre][j] * max(up[u] + 1 - i + (cntr - j), 0)) % mod;

				}

				else{

					f[now][j] = (f[now][j] + 1ll * f[pre][j] * max(up[u] + 1 - n - (k-j) - cntr , 0)) % mod;

					if(j < k) f[now][j+1] = (f[now][j+1] + 1ll * f[pre][j] * max(dn[u] - i + (cntr - j) , 0)) % mod;

				}

				f[pre][j] = 0;

			}

			if(u < n) cntr ++;

		}

		ans = (ans + (k&1?-1ll:1ll) * f[now][k]) % mod;

	}

	printf("%d\n",(ans+mod)%mod);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define nn 502

#define int long long

int n,mod;int dp[nn][nn];

int li[nn],ri[nn];

int id[nn];

#define pii pair<int,int>

#define mp make_pair

pii make(int x){

	if(li[x]) return mp(li[x]-1,ri[x]);

	else return mp(ri[x],li[x]); 

}

int cmp(int x,int y){

	return make(x)<make(y);

}

void add(int &x,int y){

	x=(x+y)%mod;

}



int solve(int diss){

	memset(dp,0,sizeof dp);dp[0][0]=1;int cnt=0,tnc=0;;

	for(int i=0;i<n;i++){

		int x=id[i];

		if(li[x]){

			for(int j=0;j<=diss;j++){

				add(dp[i+1][j],dp[i][j]*max(0ll,ri[x]+1-n/2-tnc-(diss-j)));

				add(dp[i+1][j+1],dp[i][j]*max(0ll,li[x]-j-cnt));

			}

			tnc++;

		}

		else{

			for(int j=0;j<=diss;j++) add(dp[i+1][j],dp[i][j]*max(0ll,ri[x]-j-cnt+1));

			++cnt;

		}

	}

//	cout<<dp[n][diss]<<" ";

	return dp[n][diss];

}

signed main(){

	scanf("%lld%lld",&n,&mod);n=n*2;

	for(int i=0;i<n;i++){

		li[i]=-1,ri[i]=n,id[i]=i;

		for(int j=0;j<n;j++) if(i*i+j*j<n*n/4) li[i]=j;li[i]++;

		for(int j=n-1;j>=0;j--) if(i*i+j*j>n*n) ri[i]=j;ri[i]--;

	//s	cout<<li[i]<<" "<<ri[i]<<"\n";

	}

	

	sort(id,id+n,cmp);

	

	int flg=1,ans=0;

	for(int diss=0;diss<=n/2;diss++,flg=-flg)

		ans=(ans+flg*solve(diss))%mod;

	ans+=mod;ans%=mod;

	printf("%lld\n",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.8347951663380377x):
#include <cmath>

#include <cstdio>

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;

#define N 505

#define ll long long

#define inf 0x3f3f3f3f

void Read(int &p)

{

	p = 0;

	char c = getchar();

	for (; c < '0' || c > '9'; c = getchar());

	for (; c >= '0' && c <= '9'; c = getchar())p = p * 10 + c - '0';

}

int dp[N][N], n, mod, ans;

void Upd(int &x, int y){x = x + y >= mod ? x + y - mod : x + y;}

struct Node

{

	int d, l, r;

}A[N];

bool cmp(Node A, Node B)

{

	int a1 = A.d < n ? A.l - 1 : A.r;

	int a2 = A.d < n ? A.r : A.l - 1;

	int b1 = B.d < n ? B.l - 1: B.r;

	int b2 = B.d < n ? B.r : B.l - 1;

	return a1 != b1 ? a1 < b1 : a2 != b2 ? a2 < b2 : A.d > B.d;

}

int Solve(int k)

{

	memset(dp, 0, sizeof(dp));

	int sum = 0;

	dp[0][0] = 1;

	for (int i = 1; i <= 2 * n; i++)

	{

		for (int j = 0; j <= k; j++)

		{

			if (A[i].d >= n)

			{

				int c = A[i].r + 1 - (i - 1 - (sum - j));

				if (c > 0)

					Upd(dp[i][j], 1ll * dp[i - 1][j] * c % mod);

			}

			else

			{

				int c = A[i].r + 1 - (2 * n - A[i].d - 1 + k - j);

				if (c > 0)

					Upd(dp[i][j], 1ll * dp[i - 1][j] * c % mod);

				if (j)

				{

					c = A[i].l - (i - 1 - (sum - j + 1));

					if (c > 0)

						Upd(dp[i][j], 1ll * dp[i - 1][j - 1] * c % mod);

				}

			}

		}

		sum += A[i].d < n;

	}

	return dp[n << 1][k];

}

int main()

{

	Read(n), Read(mod);

	for (int i = 0; i < 2 * n; i++)

	{

		A[i + 1].d = i;

		if (i < n)

			A[i + 1].l = ceil(sqrt(n * n - i * i));

		A[i + 1].r = floor(sqrt(4 * n * n - i * i));

		A[i + 1].r = min(A[i + 1].r, 2 * n - 1);

	}

	sort(A + 1, A + 2 * n + 1, cmp);

	for (int i = 0; i <= n; i++)

	{

		if (i & 1)

			Upd(ans, mod - Solve(i));

		else

			Upd(ans, Solve(i));

	}

	printf("%d\n", ans);

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



namespace TYC

{

	typedef long long ll;

	typedef pair<int, int> pii;

	const int N = 505;



	int n, tot, mod, F[N][N];

	pii A[N];



	int solve(const int k)

	{

		memset(F, 0, sizeof(F));

		F[0][0] = 1;

		int c1 = 0, c2 = 0;

		for (int i = 0; i < tot; i++)

			if (A[i].second == 0)

			{

				for (int j = 0; j <= c2; j++)

					F[i + 1][j] = int((ll)F[i][j] * (A[i].first + 1 - c1 - j) % mod);

				c1++;

			}

			else

			{

				for (int j = 0; j <= c2; j++)

				{

					F[i + 1][j + 1] = int((F[i + 1][j + 1] + (ll)F[i][j] * (A[i].first + 1 - c1 - j) % mod) % mod);

					F[i + 1][j] = int((F[i + 1][j] + (ll)F[i][j] * (A[i].second + 1 - n - k - (c2 - j)) % mod) % mod);

				}

				c2++;

			}

		return F[tot][k];

	}



	void work()

	{

		scanf("%d%d", &n, &mod);

		tot = n << 1;

		for (int i = 0; i < n; i++)

		{

			int l = int(ceil(sqrt((double)n * n - (double)i  * i)));

			int r = int(floor(sqrt((double)n * n * 4 - (double)i * i)));

			A[i] = {l - 1, min(tot - 1, r)};

		}

		for (int i = n; i < tot; i++)

			A[i] = {int(floor(sqrt((double)n * n * 4 - (double)i * i))), 0};



		sort(A, A + tot);

		int ans = 0;

		for (int i = 0, d = 1; i <= n; i++, d = -d)

			ans = int(((ll)ans + d * solve(i) + mod) % mod);

		printf("%d\n", ans);

	}

}



int main()

{

	TYC::work();

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4085567187709704x):
#include<bits/stdc++.h>

using namespace std;

#define re register int

#define F(x,y,z) for(re x=y;x<=z;x++)

#define FOR(x,y,z) for(re x=y;x>=z;x--)

typedef long long ll;

#define I inline void

#define IN inline int

#define C(x,y) memset(x,y,sizeof(x))

#define STS system("pause")

template<class D>I read(D &res){

	res=0;register D g=1;register char ch=getchar();

	while(!isdigit(ch)){

		if(ch=='-')g=-1;

		ch=getchar();

	}

	while(isdigit(ch)){

		res=(res<<3)+(res<<1)+(ch^48);

		ch=getchar();

	}

	res*=g;

}

struct P{

	int p,id;

	friend bool operator < (P x,P y){

		return x.p==y.p?x.id>y.id:x.p<y.p;

	}

}a[510];

int n,m,ans,l[510],r[510],f[510][300];

int main(){

    read(n);read(m);

    F(i,0,(n<<1)-1){

    	a[i].id=i;r[i]=min((n<<1)-1,(int)floor(sqrt(4*n*n-i*i)));

    	if(i>=n)l[i]=0,a[i].p=r[i];

    	else {l[i]=ceil(sqrt(n*n-i*i));a[i].p=l[i]-1;}

	}

	sort(a,a+2*n);

	F(k,0,n){

		re cnt=0;C(f,0);f[0][0]=1;

		F(i,1,n<<1){

			if(a[i-1].id<n)cnt++;

			F(j,0,min(k,cnt)){

				if(a[i-1].id<n){

					f[i][j]=(ll)f[i-1][j]*(r[a[i-1].id]+1-(2*n-1-a[i-1].id+k-j))%m;

					if(j)(f[i][j]+=(ll)f[i-1][j-1]*(a[i-1].p+1-(i-1-(cnt-j)))%m)%=m;

				}

				else f[i][j]=(ll)f[i-1][j]*(a[i-1].p+1-(i-1-(cnt-j)))%m;

			}

		}

		if(k&1)(ans-=f[2*n][k]-m)%=m;

		else (ans+=f[2*n][k])%=m;

	}

	if(ans<0)(ans%=m)+=m;

	printf("%d",ans);

   	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<iostream>

#include<cmath>

#include<algorithm>

#include<cstring>

#define cri const int

#define ll long long

using namespace std;

int dp[502][502],ans;

pair<int,int>f[502];

inline int read(int x=0){ scanf("%d",&x);return x; }

int main(){

	cri n=read(),mod=read();

	for(int i=0;i<n+n;i++){

		if(i>=n) f[i]=make_pair((int)sqrt(4*n*n-i*i)+1,0);

		else f[i]=make_pair((int)(sqrt(n*n-i*i-1e-7))+1,(int)sqrt(4*n*n-i*i)+(i!=0));

	}

	sort(f,f+n+n);

	for(int k=0;k<=n;k++){

		int c1=0,c2=0;

		memset(dp,0,sizeof dp);

		dp[0][0]=1;

		for(int i=0;i<n+n;!f[i].second?c1++:c2++,i++)

			for(int j=0;j<=k;j++){

				if(!f[i].second) dp[i+1][j]=(dp[i+1][j]+1ll*dp[i][j]*(f[i].first-j-c1))%mod;

				else{

					if(j<k) dp[i+1][j+1]=(dp[i+1][j+1]+1ll*dp[i][j]*(f[i].first-j-c1))%mod;

					dp[i+1][j]=(dp[i+1][j]+1ll*dp[i][j]*(f[i].second-n-k-(c2-j)))%mod;

				}

			}

		if(k&1) ans=(ans-dp[n+n][k]+mod)%mod;

		else ans=(ans+dp[n+n][k])%mod;

	}

	cout<<ans<<endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.380302376452118x):
#include<bits/stdc++.h>

using namespace std;

#define re register int

#define F(x,y,z) for(re x=y;x<=z;x++)

#define FOR(x,y,z) for(re x=y;x>=z;x--)

typedef long long ll;

#define I inline void

#define IN inline int

#define C(x,y) memset(x,y,sizeof(x))

#define STS system("pause")

template<class D>I read(D &res){

	res=0;register D g=1;register char ch=getchar();

	while(!isdigit(ch)){

		if(ch=='-')g=-1;

		ch=getchar();

	}

	while(isdigit(ch)){

		res=(res<<3)+(res<<1)+(ch^48);

		ch=getchar();

	}

	res*=g;

}

struct P{

	int p,id;

	friend bool operator < (P x,P y){

		return x.p==y.p?x.id>y.id:x.p<y.p;

	}

}a[510];

int n,m,ans,l[510],r[510],f[510][300];

int main(){

    read(n);read(m);

    F(i,0,(n<<1)-1){

    	a[i].id=i;r[i]=min((n<<1)-1,(int)floor(sqrt(4*n*n-i*i)));

    	if(i>=n)l[i]=0,a[i].p=r[i];

    	else {l[i]=ceil(sqrt(n*n-i*i));a[i].p=l[i]-1;}

	}

	sort(a,a+2*n);

	F(k,0,n){

		re cnt=0;C(f,0);f[0][0]=1;

		F(i,1,n<<1){

			if(a[i-1].id<n)cnt++;

			F(j,0,min(k,cnt)){

				if(a[i-1].id<n){

					f[i][j]=(ll)f[i-1][j]*(r[a[i-1].id]+1-(2*n-1-a[i-1].id+k-j))%m;

					if(j)(f[i][j]+=(ll)f[i-1][j-1]*(a[i-1].p+1-(i-1-(cnt-j)))%m)%=m;

				}

				else f[i][j]=(ll)f[i-1][j]*(a[i-1].p+1-(i-1-(cnt-j)))%m;

			}

		}

		if(k&1)(ans-=f[2*n][k]-m)%=m;

		else (ans+=f[2*n][k])%=m;

	}

	if(ans<0)(ans%=m)+=m;

	printf("%d",ans);

   	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

using namespace std;

int n,m,mod,l[525],r[525];

ll f[525][252],ans;

pair<int,int> a[525]; 

inline ll calc(int k){

	memset(f,0,sizeof(f));

	int c1=0,c2=0; f[0][0]=1;

	for(int i=1;i<=m;c1+=(a[i].second==0),c2+=(a[i].second!=0),++i) for(int j=0;j<=i&&j<=k;++j){

		if(a[i].second==0) (f[i][j]+=f[i-1][j]*(a[i].first-c1-j))%=mod; 

		else{

			if(j) (f[i][j]+=f[i-1][j-1]*(a[i].first-j-c1+1))%=mod;

			(f[i][j]+=f[i-1][j]*(a[i].second-k-n-(c2-j)))%=mod;

		}

	}

	return f[m][k];

}

int main(){

	cin>>n>>mod; m=n<<1;

	for(int i=0;i<m;++i){

		l[i]=0; r[i]=m-1;

		while(l[i]<m&&l[i]*l[i]+i*i<n*n) ++l[i];

		while(r[i]>0&&r[i]*r[i]+i*i>m*m) --r[i];

		--l[i];

		if(i<n) a[i+1]=make_pair(l[i]+1,r[i]+1);

		else a[i+1]=make_pair(r[i]+1,0);

	}

	sort(a+1,a+m+1);

	for(int i=0;i<=n;++i) (ans+=((i&1)?-1:1)*calc(i))%=mod;

	cout<<(ans%mod+mod)%mod<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.6043499391747507x):
#include<bits/stdc++.h>

using namespace std;

#define re register int

#define F(x,y,z) for(re x=y;x<=z;x++)

#define FOR(x,y,z) for(re x=y;x>=z;x--)

typedef long long ll;

#define I inline void

#define IN inline int

#define C(x,y) memset(x,y,sizeof(x))

#define STS system("pause")

template<class D>I read(D &res){

	res=0;register D g=1;register char ch=getchar();

	while(!isdigit(ch)){

		if(ch=='-')g=-1;

		ch=getchar();

	}

	while(isdigit(ch)){

		res=(res<<3)+(res<<1)+(ch^48);

		ch=getchar();

	}

	res*=g;

}

struct P{

	int p,id;

	friend bool operator < (P x,P y){

		return x.p==y.p?x.id>y.id:x.p<y.p;

	}

}a[510];

int n,m,ans,l[510],r[510],f[510][300];

int main(){

    read(n);read(m);

    F(i,0,(n<<1)-1){

    	a[i].id=i;r[i]=min((n<<1)-1,(int)floor(sqrt(4*n*n-i*i)));

    	if(i>=n)l[i]=0,a[i].p=r[i];

    	else {l[i]=ceil(sqrt(n*n-i*i));a[i].p=l[i]-1;}

	}

	sort(a,a+2*n);

	F(k,0,n){

		re cnt=0;C(f,0);f[0][0]=1;

		F(i,1,n<<1){

			if(a[i-1].id<n)cnt++;

			F(j,0,min(k,cnt)){

				if(a[i-1].id<n){

					f[i][j]=(ll)f[i-1][j]*(r[a[i-1].id]+1-(2*n-1-a[i-1].id+k-j))%m;

					if(j)(f[i][j]+=(ll)f[i-1][j-1]*(a[i-1].p+1-(i-1-(cnt-j)))%m)%=m;

				}

				else f[i][j]=(ll)f[i-1][j]*(a[i-1].p+1-(i-1-(cnt-j)))%m;

			}

		}

		if(k&1)(ans-=f[2*n][k]-m)%=m;

		else (ans+=f[2*n][k])%=m;

	}

	if(ans<0)(ans%=m)+=m;

	printf("%d",ans);

   	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int N=510;

typedef long long ll;

int dp[N/2][N],mod,jc[N],n,L[N],R[N],ans,id[N];

int main(){

	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

	cin>>n>>mod;

	jc[0]=1;

	for(int i=1;i<=2*n;++i)jc[i]=(ll)jc[i-1]*i%mod;

	for(int i=0;i<2*n;++i){

		for(L[i]=0;i*i+L[i]*L[i]<n*n;++L[i]);

		for(R[i]=1;i*i+(2*n-R[i])*(2*n-R[i])>2*n*2*n;++R[i]);--R[i];

		id[i]=i;

	}

	for(int i=n;i<2*n;++i)L[i]=2*n-R[i];

	for(int i=0;i<2*n;++i)

		for(int j=i+1;j<2*n;++j)

			if(L[i]<L[j]||(L[i]==L[j]&&R[i]>R[j])){swap(L[i],L[j]);swap(R[i],R[j]);swap(id[i],id[j]);}

	for(int i=n;i<=2*n;++i){

		memset(dp,0,sizeof dp);dp[0][0]=1;

		int cnt=0,lim1=0,lim2=0;

		for(int j=0;j<2*n;++j)if(id[j]>=n){

			++cnt;lim1=min(lim1+1,i);

			for(int l=lim2;l>=0;--l){

				for(int k=lim1;k>=cnt;--k)

					dp[l][k]=dp[l][k-1]?(ll)dp[l][k-1]*(L[j]-(i-k))%mod:0;

				dp[l][cnt-1]=0;

			}

		}else{

			lim1=min(lim1+1,i);lim2=min(lim2+1,2*n-i);

			for(int l=lim2;l>=0;--l)for(int k=lim1;k>=cnt;--k)

				{

					if(dp[l][k-1])dp[l][k]=(dp[l][k]+(ll)dp[l][k-1]*(L[j]-(i-k)))%mod;

					if(l&&dp[l-1][k])dp[l][k]=(dp[l][k]+(ll)dp[l-1][k]*(R[j]-l+1))%mod;

				}

		}

		for(int j=0;j<=2*n-i;++j){

			int ret=(ll)dp[j][i]*jc[2*n-i-j]%mod;

			if((i-n+j)&1)ans=(ans-ret+mod)%mod;

			else ans=(ans+ret)%mod;

		}

	}

	cout<<ans<<'\n';

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.170291232479687x):
#include<bits/stdc++.h>

using namespace std;

const int N=510;

typedef long long ll;

int dp[N],mod,jc[N],n,L[N],R[N],ans,id[N];

int main(){

	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

	cin>>n>>mod;

	jc[0]=1;

	for(int i=1;i<=2*n;++i)jc[i]=(ll)jc[i-1]*i%mod;

	for(int i=0;i<2*n;++i){

		for(L[i]=0;i*i+L[i]*L[i]<n*n;++L[i]);

		for(R[i]=2*n-1;i*i+R[i]*R[i]>2*n*2*n;--R[i]);

		id[i]=i;

	}

	for(int i=n;i<2*n;++i)L[i]=R[i]+1;

	for(int i=0;i<2*n;++i)

		for(int j=i+1;j<2*n;++j)

			if(L[i]>L[j]||(L[i]==L[j]&&R[i]>R[j])){swap(L[i],L[j]);swap(R[i],R[j]);swap(id[i],id[j]);}

	for(int i=n;i<=2*n;++i){

		memset(dp,0,sizeof dp);dp[0]=1;

		int cnt=0,lim1=0;

		for(int j=0;j<2*n;++j)if(id[j]>=n){

			++cnt;lim1=min(lim1+1,i);

			for(int k=lim1;k>=cnt;--k)

				dp[k]=dp[k-1]?(ll)dp[k-1]*(L[j]-k+1)%mod:0;

			dp[cnt-1]=0;

		}else{

			lim1=min(lim1+1,i);

			for(int k=lim1;k>cnt;--k)dp[k]=((ll)dp[k]*(R[j]-(i+j-k)+1)+(ll)dp[k-1]*(L[j]-k+1))%mod;

			dp[cnt]=(ll)dp[cnt]*(R[j]-(i+j-cnt)+1)%mod;

		}

		if((i-n)&1)ans=(ans-dp[i]+mod)%mod;

		else ans=(ans+dp[i])%mod;

	}

	cout<<ans<<'\n';

	return 0;

}