Under construction

Still being built. Things may change

Parentheses

Time limit1sMemory limit512 MB

Summary
Count parentheses strings of length n by their reversal-and-flip operation distance, then output the weighted sum of those counts modulo m.
Level

Hard9 of 10

Topics
Combinatorics, Math, Dynamic programming
Solved
No attempts yet

Problem

A correct parentheses sequence is defined recursively as follows:

  • The empty string is a correct sequence.
  • If XX and YY are correct sequences, then XYX Y (the concatenation of XX and YY) is a correct sequence.
  • If XX is a correct sequence, then ‘(‘X‘)‘`(`X`)` is a correct sequence.

Every correct parentheses sequence can be derived using the rules above.

For a parentheses sequence, you can perform some operations on it.

  • Each time, choose two indices LL and RR such that L≤RL \le R. The operation modifies the characters at indices from LL to RR, inclusive.
  • First, the order of these characters is reversed.
  • Then, each character is toggled to the opposite one. That is, each '(' in the range becomes ')' and each ')' becomes '('.

The value of a parentheses sequence is the minimum number of operations required to change it into a correct parentheses sequence. If it is impossible, the value is 1010010^{100}.

For example, the value of "()((" is 11, the value of "()()" is 00, and the value of "(((" is 1010010^{100}.

You are given an integer nn. For each 1≤i≤n1 \le i \le n, find the number AiA_i of distinct parentheses sequences of length nn whose value is ii, then compute the sum ∑i=0n((i+1)⋅Ai)\sum_{i = 0}^{n} ((i + 1) \cdot A_i).

The answer may be very large, so print it modulo the given integer mm.

Input

The first line contains two integers nn and mm (1≤n≤1061 \le n \le 10^6, 1≤m≤1091 \le m \le 10^9).

Output

Print one integer: the answer to the problem.

Examples2

  1. Example 1

    Input
    1 100
    
    Expected output
    0
    
  2. Example 2

    Input
    10 100
    
    Expected output
    68