#P8556. 心跳 加强版

    ID: 9815 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>数学多项式洛谷原创O2优化组合数学线性递推洛谷月赛

心跳 加强版

Background

This problem is an enhanced version of Luogu September Monthly Contest II & NR I. E. Heartbeat. The only difference is that the constraints are changed to n5×106n \le 5 \times {10}^6.


“From the clear beating sound, overlapping echoes and flowing longing are conveyed.

Let’s promise never to be apart again, and hope that no matter when, you will not be left lonely.”

When people are in love, their mood does not stay the same forever. Joy and sadness will become calm as time passes. What is most unforgettable is the feeling of “being moved,” the pleasant surprise from things never experienced before. Therefore, sometimes, losing some especially beautiful memories can instead make that thrilling feeling happen more often. But is it really worth losing those memories for that?

Problem Description

Herlde wants to explore the question above. She wants to do some statistics first, so she abstracts the problem.

For a sequence pp of length ll, we define the function:

  • f(p)f(p) is the number of indices 1il1 \le i \le l such that pi=maxj=1ipjp_i=\max_{j=1}^i p_j (that is, the number of prefix maximums).

Now, given n,mn, m, find how many sequences aa of length nn with values in [m,n][m,n] satisfy:

  • There exists a permutation pp such that: let PiP_i denote the sequence obtained from pp by removing pip_i (i.e. [p1,p2,,pi1,pi+1,,pn][p_1,p_2,\dots,p_{i-1},p_{i+1},\dots,p_n]), and f(Pi)=aif(P_i)=a_i.

The answer should be taken modulo 109+710^9+7.

Input Format

One line with two positive integers n,mn, m.

Output Format

One line with one integer, representing the answer.

3 1

6

5 3

8

500000 100000

226048544

Hint

[Sample Explanation #2]

There are 88 different possible aa:

  1. {4,4,4,4,4}\{4,4,4,4,4\}, one corresponding pp is {1,2,3,4,5}\{1,2,3,4,5\}.
  2. {3,3,3,4,4}\{3,3,3,4,4\}, one corresponding pp is {1,2,3,5,4}\{1,2,3,5,4\}.
  3. {3,3,4,4,3}\{3,3,4,4,3\}, one corresponding pp is {1,2,4,3,5}\{1,2,4,3,5\}.
  4. {3,3,3,3,4}\{3,3,3,3,4\}, one corresponding pp is {1,2,4,5,3}\{1,2,4,5,3\}.
  5. {3,4,4,3,3}\{3,4,4,3,3\}, one corresponding pp is {1,3,2,4,5}\{1,3,2,4,5\}.
  6. {3,3,3,4,3}\{3,3,3,4,3\}, one corresponding pp is {1,3,4,2,5}\{1,3,4,2,5\}.
  7. {4,4,3,3,3}\{4,4,3,3,3\}, one corresponding pp is {2,1,3,4,5}\{2,1,3,4,5\}.
  8. {3,3,4,3,3}\{3,3,4,3,3\}, one corresponding pp is {2,3,1,4,5}\{2,3,1,4,5\}.

[Constraints]

For all testdata, it is guaranteed that 1m<n5×1061 \le m < n \le 5 \times {10}^6.


Herlde successfully computed the number of different kinds of love. But she will only experience one of them.

Translated by ChatGPT 5