#P7986. [USACO21DEC] HILO P

[USACO21DEC] HILO P

Problem Description

Bessie has a number x+0.5x+0.5, where xx is an integer between 00 and NN (1N50001\le N\le 5000).

Elsie is trying to guess this number. She may ask questions of the following form for some integer between 11 and NN: “Is ii too high or too low?” If ii is greater than x+0.5x+0.5, Bessie answers "HI!". If ii is less than x+0.5x+0.5, she answers "LO!".

Elsie comes up with the following strategy for guessing Bessie’s number. Before making any guesses, she creates a sequence containing NN integers in which every number from 11 to NN appears exactly once (in other words, the sequence is a permutation of length NN). Then she goes through this list and makes guesses in the order of the numbers in the list. However, Elsie will skip all unnecessary guesses. That is, if Elsie is about to guess some number ii, and earlier she has already guessed some j<ij < i and Bessie answered "HI!", then Elsie will not guess ii and will instead move on to the next number in the sequence. Similarly, if she is about to guess some number ii, and earlier she has already guessed some j>ij > i and Bessie answered "LO!", then Elsie will not guess ii and will instead move on to the next number in the sequence. It can be proven that using this strategy, for any sequence Elsie creates, she can uniquely determine xx.

If we concatenate all of Bessie’s answers "HI" or "LO" into a string SS, then the number of times Bessie says "HILO" is the number of length 44 substrings of SS that are equal to "HILO".

Bessie knows that Elsie will use this strategy and has already chosen the value xx, but she does not know what permutation Elsie will use. Your goal is to compute, over all permutations Elsie could choose, the sum of the number of times Bessie says "HILO", modulo 109+710^9+7.

Input Format

The input consists of one line containing NN and xx.

Output Format

Output the total number of "HILO", modulo 109+710^9+7.

4 2
17
60 10
508859913

Hint

[Sample Explanation 1]

In this test case, Bessie’s number is 2.52.5.

For example, if Elsie’s permutation is (4,1,3,2)(4,1,3,2), then Bessie will say "HILOHILO", for a total of 22 occurrences of "HILO". As another example, if Elsie’s permutation is (3,1,2,4)(3,1,2,4), then Bessie will say "HILOLO", for a total of 11 occurrence of "HILO".

[Sample Explanation 2]

Make sure to output the sum modulo 109+710^9+7.

[Constraints]

  • Test cases 33-1010 satisfy N50N\le 50.
  • Test cases 1111-1818 satisfy N500N\le 500.
  • Test cases 1919-2626 have no additional constraints.

Translated by ChatGPT 5