Problem:
Score : 1800 points

Problem Statement

Given is an integer N.
How many permutations (P_0,P_1,\cdots,P_{2N-1}) of (0,1,\cdots,2N-1) satisfy the following condition?

For each i (0 \leq i \leq 2N-1), N^2 \leq i^2+P_i^2 \leq (2N)^2 holds.

Since the number can be enormous, compute it modulo M.

Constraints

1 \leq N \leq 250

2 \leq M \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of permutations that satisfy the condition, modulo M.

Sample Input 1

2 998244353

Sample Output 1

4

Four permutations satisfy the condition:

(2,3,0,1)

(2,3,1,0)

(3,2,0,1)

(3,2,1,0)

Sample Input 2

10 998244353

Sample Output 2

53999264

Sample Input 3

200 998244353

Sample Output 3

112633322

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
using namespace std;
const int N=505;
int n,mo,A[N],B[N];
int v[N],id[N],f[2][N][N];
int main(){
	scanf("%d%d",&n,&mo);
	for (int i=0,j=2*n-1;i<2*n;i++){
		for (;i*i+j*j>4*n*n;--j);
		B[i]=v[i]=j+1; id[i]=i;
	}
	for (int i=0,j=n;i<n;i++){
		for (;i*i+j*j>=n*n;--j);
		A[i]=v[i]=j+1;
	}
	For(i,0,2*n-1)
		For(j,i+1,2*n-1)
			if (v[id[i]]>v[id[j]]||v[id[i]]==v[id[j]]&&B[id[i]]>B[id[j]])
				swap(id[i],id[j]);
	int S1=0,S2=0;
	For(i,0,n) f[0][0][n-i]=1;
	For(i,0,2*n-1)
		if (id[i]>=n){
			For(j,0,S2) For(k,0,n-S2)
				f[0][j][k]=1ll*f[0][j][k]*(B[id[i]]-(S1+j))%mo;
			++S1;
		}
		else{
			For(j,0,S2) For(k,0,n-S2){
				f[1][j+1][k]=(f[1][j+1][k]+mo-1ll*f[0][j][k]*(A[id[i]]-(S1+j))%mo)%mo;
				if (k) f[1][j][k-1]=(f[1][j][k-1]+1ll*f[0][j][k]*(B[id[i]]-(2*n-k)))%mo;
			}
			++S2;
			For(j,0,S2) For(k,0,n-S2)
				f[0][j][k]=f[1][j][k],f[1][j][k]=0;
		}
	int ans=0;
	For(i,0,n)
		ans=(ans+f[0][i][0])%mo;
	printf("%d\n",ans);
}