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>
using namespace std;
const int N=510;
typedef pair<int,int> pii;
#define mp make_pair
#define fi first
#define se second

int n,mod,f[N][N],tp=0,g[N],new_f[N];
pii st[N];

int cal(int x,int y,int c) {
	int z=0;
	while(z<(n<<1)&&z*z+x*x<=y*y+c) ++z;
	return z;
}

int main() {
	cin>>n>>mod;
	for(int i=0;i<n;i++) st[++tp]=mp(cal(i,n,-1),-i),g[i]=cal(i,n<<1,0);
	for(int i=n;i<(n<<1);i++) st[++tp]=mp(cal(i,n<<1,0),1);
	sort(st+1,st+tp+1);
	int ans=0;
	for(int k=0;k<=n;k++) {
		memset(f,0,sizeof(f)),f[0][0]=1;
		int cnta=0,cntc=0;
		for(int i=1;i<=tp;i++) {
			if(st[i].se>0) {
				for(int j=0;j<=cnta;j++) f[i][j]=1ll*f[i-1][j]*(st[i].fi-cntc-j)%mod;
				++cntc;
			}
			else {
				memset(new_f,0,sizeof(new_f));
				for(int j=0;j<=cnta;j++) {
					new_f[j+1]=(new_f[j+1]+1ll*f[i-1][j]*(st[i].fi-cntc-j))%mod;
					new_f[j]=(new_f[j]+1ll*f[i-1][j]*(g[-st[i].se]-(n+k+cnta-j)))%mod;
				}
				memcpy(f[i],new_f,sizeof(new_f));
				++cnta;
			}
		}
		ans=(ans+((k&1)?-1:1)*f[tp][k])%mod;
	}
	cout<<(ans%mod+mod)%mod;
	return 0;
}