問題一覧 > 通常問題

No.271 next_permutation (2)

レベル : / AI推定難易度 : 2475 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 19
作問者 : anta
お気に入りにしたユーザー ProblemId : 336 / 自分の提出
問題文最終更新日: 2026-06-25 03:16:42
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 118 (順位表) の他の問題:

Problem Statement

For a permutation $x$ of $[1,2,\ldots,n]$ for some $n$, let $f(x)$ be the permutation that comes right after $x$ in lexicographic order. However, if $x$ is the lexicographically last permutation $[n,n-1, \ldots, 1]$, then $f(x)$ is defined as the lexicographically first permutation $[1,2,\ldots,n]$. For example, $f([1, 2, 3]) = [1, 3, 2]$ and $f(f([3,1,2])) = f([3,2,1]) = [1,2,3]$.

Let ${\rm inv}(x)$ denote the inversion number of a permutation $x$. For example, ${\rm inv}([2,1,3]) = 1, {\rm inv}([3,2,1]) = 3$.

Given a permutation $p$, define a sequence of permutations $A$ by $A_0 = p$, $A_i = f(A_{i-1})$ ($i \in [1,2,\ldots]$). In other words, $A_i$ is the result of applying $f$ to $p$ $i$ times. For example, if $p = [2,1,3]$, then $A = [[2,1,3],[2,3,1],[3,1,2],[3,2,1],[1,2,3],\dots]$. Note that the indices of $A$ start from 0.

You are given an integer $N$, an integer $K$, and a permutation $p$ of length $N$ as input. Find $\displaystyle \sum_{i=0}^{K-1} {\rm inv}(A_i)$ modulo $1000000007$.

Input

$N$ $K$
$p_1$ $p_2$ $\ldots$ $p_N$

The first line contains integers $N (1 \le N \le 10^5)$ and $K (0 \le K \le 10^{18})$, separated by a space. The second line contains a permutation $p (1 \le p_i \le N)$ of $[1,2,\ldots,N]$ of length $N$, separated by spaces.

Output

Output $\displaystyle \sum_{i=0}^{K-1} {\rm inv}(A_i)$ modulo $1000000007$ on a single line.

Sample

Sample 1
Input
3 5
2 1 3
Output
8

This is the example of $A$ given in the problem statement. The values of ${\rm inv}$ are $[1, 2, 2, 3, 0]$, so the answer is their sum, $8$.

Sample 2
Input
3 36
1 2 3
Output
54

In this case, $A$ cycles with period $3! = 6$. The sum of ${\rm inv}$ over one period is $9$, so the answer is $9 \times \frac{36}{6} = 54$.

Sample 3
Input
1 1000000000000000000
1
Output
0

Sample 4
Input
2 1000000000000000000
1 2
Output
500000028

Note that you must output the remainder modulo $1000000007$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。