Loading [MathJax]/jax/output/HTML-CSS/jax.js

XORRAY - 2D arrays with XOR property

We consider 2D arrays A, (0,0)-indexed, shape N×M.
With 0i<N and 0j<M, we have 0<Ai,jN×M.
Our interest will be to count those arrays that have the two properties :

  • Arrays A are composed with all numbers from 1 to N×M.
    i.e. we have (i,j)(k,l)Ai,jAk,l
  • (ij)>(kl)Ai,j>Ak,l, where denotes bitwise XOR.

Input

The first line contains T, the number of test cases, and P a prime number.

Each of the next T lines contains N and M, the shape of the arrays A.

Output

For each test case, print the number of arrays A with the given properties.
As the result may be large, the answer modulo P is required.

Example

Input:
2 1000000007
2 2
997 799

Output:
4
828630475

For the first case, the 4 possible 2x2 arrays are : (1342), (1432), (2341), and (2431).

Constraints

1T104,
109<P<2×109, a prime number,
1N109,
1M105.

Constraints allow a small kB of unoptimized PY3.4 code to get AC in the third of the TL. Have fun.


Added by:Francky
Date:2016-06-28
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU JS-MONKEY
Resource:VECTAR1

hide comments
2016-10-08 21:22:34 :D
Please keep in mind that this problem and VECTAR1 have a significant difference outside of constraints. Array indexing in VECTAR1 is in range <1;D> and in XORARRAY <0;D-1> (D standing for W or H). Both problems are of course correctly described, but it's easy to miss.
2016-08-15 22:46:32 :D
Great Francky-styled problem. Math / computation - centric and very interesting to solve.

=(Francky)=> Congrats for #1 rank, and many thanks for your appreciation.

Last edit: 2016-08-20 12:21:28
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.