See the original article https://dyingdown.github.io/2019/11/22/AtCoder-5165-Kleene-Inversion/
Kleene Inversion
Problem Statement
We have a sequence of N integers A = A 0 , A 1 , ⋯ , A N − 1 A~=~ A_0,~A_1,~ \cdots,~A_{N−1} A = A0, A1, ⋯, AN−1.
Let B be a sequence of K × N K \times N K×N integers obtained by concatenating K K K copies of A A A. For example, if A = 1 , 3 , 2 A ~=~1,~3,~2 A = 1, 3, 2 and K = 2 , B = 1 , 3 , 2 , 1 , 3 , 2 K ~=~2, B~=~1,~3,~2,~1,~3,~2 K = 2,B = 1, 3, 2, 1, 3, 2.
Find the inversion number of B, modulo 1 0 9 + 7 10^9+7 109+7.
Here the inversion number of B is defined as the number of ordered pairs of integers ( i , j ) ( 0 ≤ i < j ≤ K × N − 1 ) (i,~j)~(0≤i<j≤K \times N−1) (i,