New Year and Permutation

Time limit1sMemory limit1024 MB

Summary
Count over all n! permutations the total number of segments whose max minus min equals length minus one, modulo a prime m.
Level

Medium7 of 10

Topics
Combinatorics, Math, Dynamic programming, Implementation
Solved
No attempts yet

Problem

A permutation is an array of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is a 44 in the array).

A sequence aa is a subsegment of a sequence bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We denote the subsegments as [l,r][l, r], where l,rl, r are two integers with 1≤l≤r≤n1 \le l \le r \le n. This indicates the subsegment where l−1l-1 elements from the beginning and n−rn-r elements from the end are deleted from the sequence.

For a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n, we define a framed segment as a subsegment [l,r][l,r] where max⁡{pl,pl+1,…,pr}−min⁡{pl,pl+1,…,pr}=r−l\max\{p_l, p_{l+1}, \dots, p_r\} - \min\{p_l, p_{l+1}, \dots, p_r\} = r - l. For example, for the permutation (6,7,1,8,5,3,2,4)(6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are [1,2],[5,8],[6,7],[3,3],[8,8][1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i][i,i] is always a framed segment for any ii between 11 and nn, inclusive.

We define the happiness of a permutation pp as the number of pairs (l,r)(l, r) such that 1≤l≤r≤n1 \le l \le r \le n and [l,r][l, r] is a framed segment. For example, the permutation [3,1,2][3, 1, 2] has happiness 55: all segments except [1,2][1, 2] are framed segments.

Given integers nn and mm, Jongwon wants to compute the sum of happiness over all permutations of length nn, modulo the prime number mm. There are n!n! (factorial of nn) different permutations of length nn.

Input

The only line contains two integers nn and mm (1≤n≤250 0001 \le n \le 250\,000, 108≤m≤10910^8 \le m \le 10^9, mm is prime).

Output

Print rr (0≤r<m0 \le r < m), the sum of happiness over all permutations of length nn, modulo the prime number mm.

Hint

For sample input n=3n=3, consider all permutations of length 33:

  • [1,2,3][1, 2, 3]: all subsegments are framed segments. Happiness is 66.
  • [1,3,2][1, 3, 2]: all subsegments except [1,2][1, 2] are framed segments. Happiness is 55.
  • [2,1,3][2, 1, 3]: all subsegments except [2,3][2, 3] are framed segments. Happiness is 55.
  • [2,3,1][2, 3, 1]: all subsegments except [2,3][2, 3] are framed segments. Happiness is 55.
  • [3,1,2][3, 1, 2]: all subsegments except [1,2][1, 2] are framed segments. Happiness is 55.
  • [3,2,1][3, 2, 1]: all subsegments are framed segments. Happiness is 66.

Thus, the sum of happiness is 6+5+5+5+5+6=326+5+5+5+5+6 = 32.

Examples5

  1. Example 1

    Input
    1 993244853
    
    Expected output
    1
    
  2. Example 2

    Input
    2 993244853
    
    Expected output
    6
    
  3. Example 3

    Input
    3 993244853
    
    Expected output
    32
    
  4. Example 4

    Input
    2019 993244853
    
    Expected output
    923958830
    
  5. Example 5

    Input
    2020 437122297
    
    Expected output
    265955509