Under construction

Still being built. Things may change

Festivals in JOI Kingdom 2

Time limit6sMemory limit1024 MB

Summary
Count the pairs of sequences a and b of N intervals using each value from 1 to 2N once, where greedy selection by start time is not optimal, modulo prime P.
Level

Hard9 of 10

Topics
Dynamic programming, Combinatorics, Greedy
Solved
No attempts yet

Problem

In JOI Kingdom, a national festival is held once a year. During the festival, there are NN events in total, and the schedule of each event is already fixed. The schedules of the NN events are described by sequences aa and bb of length NN that satisfy the following conditions.

  • Every integer from 11 to 2N2N, inclusive, appears as an element of aa or bb.
  • ai<bia_i < b_i (1≤i≤N1 \le i \le N).
  • ai<ai+1a_i < a_{i+1} (1≤i≤N−11 \le i \le N-1).

The ii-th event starts at aia_i minutes after the beginning of the festival and ends at bib_i minutes after the beginning of the festival.

Participants may choose any events they like. However, they may not take part in two events whose schedules overlap. (The starting times and ending times of all events are different from each other.)

JOI-kun wants to take part in as many events as possible. Until last year, he chose events on a computer with the following procedure.

  • For i=1,2,…,Ni = 1, 2, \dots, N, do the following in this order.
    • If the schedule of the ii-th event does not overlap the schedules of the events he has already chosen, he takes part in the ii-th event. Otherwise, he does not.

After studying computer science, JOI-kun noticed that this procedure does not always maximize the number of events he takes part in. Starting this year, he will use an improved algorithm. With the improved algorithm, JOI-kun will be able to maximize the number of events he takes part in.

JOI-kun wants to know the number of cases in which the improved algorithm produces a larger number of events.

Given the integer NN and a large prime PP, write a program that counts the pairs of sequences aa and bb describing the schedules for which the improved algorithm produces a larger number of events. Output the count modulo PP.

Input

Read the following data from standard input.

NN PP

Output

Write one line to standard output. The line contains the number of pairs of sequences aa and bb describing the schedules of the NN events for which the improved algorithm produces a larger number of events, modulo PP.

Constraints

  • 1≤N≤20 0001 \le N \le 20\,000.
  • 108<P<10910^8 < P < 10^9.
  • PP is a prime number.
  • All given values are integers.

Examples3

  1. Example 1

    Input
    3 100000007
    
    Expected output
    2
    
  2. Example 2

    Input
    4 100000007
    
    Expected output
    28
    
  3. Example 3

    Input
    15 999999937
    
    Expected output
    935834920