factorial-again-1

Kate has finally calmed down and decides to forgive Little Deepu, but she won't forgive him just like that. She agrees to forgive him on the grounds that he can solve a mathematical question for her. 
She gives Deepu a large number N and a prime number P and asks him to calculate ((3*N)! / (3!^N) )%P.Your task is to help Little Deepu get back together with Kate.

Input
First line contains number of test cases T.
Next T lines contain two integers N and P, where P is a prime.  

Output
For each test case output the result ( (3*N)! / (3!)^N ) % P.  

Constraints:
1 ≤ T ≤ 10000
1 ≤ N ≤ 333333333
1 ≤ P ≤ 1000000000
1 ≤ abs(3*N-P) ≤ 1000  

SAMPLE INPUT
2
3 11
2 11

SAMPLE OUTPUT
8
9

Source: hackerearth | Difficulty: MEDIUM
Tags: Number theory

Mathematics


Solutions:
from sys import stdin
import math
t = int(stdin.readline())
fa = [1]
for i in range(1,100):
	fa.append(i*fa[i-1])
for _ in range(t):
	n,p = list(map(int,stdin.readline().split()))
	if p==2:
		num = fa[3*n]
		den = pow(6,n,p)
		print((num*den)%2)
	else:
		den = pow(6,n,p)
		den = pow(den,p-2,p)
		num = 0
		if 3*n <p:
			num = 1
			for i in range(3*n+1,p):
				num = (num * i)%p
			num = pow(num, p-2,p)
		print((num * (p-1) * den)%p)