New Year and Permutation
Time limit1sMemory limit1024 MB
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 distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array) and is also not a permutation ( but there is a in the array).
A sequence is a subsegment of a sequence if can be obtained from 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 , where are two integers with . This indicates the subsegment where elements from the beginning and elements from the end are deleted from the sequence.
For a permutation , we define a framed segment as a subsegment where . For example, for the permutation some of its framed segments are . In particular, a subsegment is always a framed segment for any between and , inclusive.
We define the happiness of a permutation as the number of pairs such that and is a framed segment. For example, the permutation has happiness : all segments except are framed segments.
Given integers and , Jongwon wants to compute the sum of happiness over all permutations of length , modulo the prime number . There are (factorial of ) different permutations of length .
Input
The only line contains two integers and (, , is prime).
Output
Print (), the sum of happiness over all permutations of length , modulo the prime number .
Hint
For sample input , consider all permutations of length :
- : all subsegments are framed segments. Happiness is .
- : all subsegments except are framed segments. Happiness is .
- : all subsegments except are framed segments. Happiness is .
- : all subsegments except are framed segments. Happiness is .
- : all subsegments except are framed segments. Happiness is .
- : all subsegments are framed segments. Happiness is .
Thus, the sum of happiness is .