Under construction

Still being built. Things may change

Interval-Free Permutations

Time limit2sMemory limit512 MB

Summary
Count permutations of length n with no contiguous block of length 2 to n-1 whose values form consecutive integers, modulo a prime p.
Level

Hard9 of 10

Topics
Combinatorics, Dynamic programming, Math
Solved
No attempts yet

Problem

Consider a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n of the integers from 11 to nn. A sub-segment pl,pl+1,…,prp_l, p_{l+1}, \ldots, p_r is called an interval if it is a reordering of some set of consecutive integers. For example, the permutation (6,7,1,8,5,3,2,4)(6, 7, 1, 8, 5, 3, 2, 4) has the intervals (6,7)(6, 7), (5,3,2,4)(5, 3, 2, 4), (3,2)(3, 2), and others.

Every permutation has trivial intervals: the full permutation and every single element. A permutation is interval-free if it has no non-trivial intervals, meaning no intervals of length between 22 and n−1n-1 inclusive.

Count the interval-free permutations of length nn and print the result modulo the prime pp.

Input

The first line contains two integers tt (1≤t≤4001 \le t \le 400) and pp (108≤p≤10910^8 \le p \le 10^9), the number of test cases and the prime modulo. Each of the next tt lines contains one integer nn (1≤n≤4001 \le n \le 400), the length of the permutation.

Output

For each test case, print a single integer: the number of interval-free permutations of length nn modulo pp.

Hint

For n=1n = 1, the only permutation is interval-free. For n=4n = 4, the interval-free permutations are (2,4,1,3)(2, 4, 1, 3) and (3,1,4,2)(3, 1, 4, 2). For n=5n = 5, they are (2,4,1,5,3)(2, 4, 1, 5, 3), (2,5,3,1,4)(2, 5, 3, 1, 4), (3,1,5,2,4)(3, 1, 5, 2, 4), (3,5,1,4,2)(3, 5, 1, 4, 2), (4,1,3,5,2)(4, 1, 3, 5, 2), and (4,2,5,1,3)(4, 2, 5, 1, 3). For n=9n = 9 there are 2814628146 of them, so the full list is omitted. Examples include (4,7,9,5,1,8,2,6,3)(4, 7, 9, 5, 1, 8, 2, 6, 3), (2,4,6,1,9,7,3,8,5)(2, 4, 6, 1, 9, 7, 3, 8, 5), (3,6,9,4,1,5,8,2,7)(3, 6, 9, 4, 1, 5, 8, 2, 7), and (8,4,9,1,3,6,2,7,5)(8, 4, 9, 1, 3, 6, 2, 7, 5).

Examples2

  1. Example 1

    Input
    4 998244353
    1
    4
    5
    9
    
    Expected output
    1
    2
    6
    28146
    
  2. Example 2

    Input
    1 437122297
    20
    
    Expected output
    67777575