Under construction

Still being built. Things may change

Fractal Sequence

Interview

Time limit1sMemory limit1024 MB

Summary
Count the sequences A of N distinct positive integers whose sorted version B satisfies the fixed-point condition, printed modulo M.
Level

Medium4 of 10

Topics
Math, Combinatorics
Solved
No attempts yet

Problem

There is a sequence AA of NN distinct positive integers. Let BB be the sequence obtained by sorting AA in ascending order. Using BB, define a sequence CC of length NN as follows.

ci={bbiif 1≤bi≤N0otherwisec_i = \begin{cases} b_{b_i} & \text{if } 1 \le b_i \le N \\ 0 & \text{otherwise} \end{cases}

Here, for every ii with 1≤i≤N1 \le i \le N, bib_i is the ii-th element of BB, and cic_i is the ii-th element of CC.

A sequence AA is called a fractal sequence if B=CB = C. Given NN and MM, find the number of distinct fractal sequences of length NN, modulo MM.

Input

The first line contains two integers NN (1≤N≤1031 \le N \le 10^3) and MM (1≤M≤1091 \le M \le 10^9), separated by a space.

Output

Print the number of distinct fractal sequences of length NN, modulo MM.

Hint

Two sequences XX and YY are equal if and only if the following holds.

  • XX and YY have the same length.
  • If that length is NN, then for every ii with 1≤i≤N1 \le i \le N, the ii-th element xix_i of XX equals the ii-th element yiy_i of YY.

If two sequences XX and YY are equal, this is written as X=YX = Y.

Examples2

  1. Example 1

    Input
    2 10
    
    Expected output
    2
    
  2. Example 2

    Input
    1 10
    
    Expected output
    1