Problem:
Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

He will freely choose an integer b.
Here, he will get sad if A_i and b+i are far from each other.
More specifically, the sadness of Snuke is calculated as follows:

abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))

Here, abs(x) is a function that returns the absolute value of x.

Find the minimum possible sadness of Snuke.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

If we choose b=0, the sadness of Snuke would be abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2.
Any choice of b does not make the sadness of Snuke less than 2, so the answer is 2.

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,K,M;
int A[252525];
int cnt[404];
ll mo=1000000007;

ll dp[25205][2][404], dp2[2][404];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K>>M;
	FOR(i,M) cin>>A[i], A[i]--;
	
	ll tot=(N-M+1);
	FOR(i,N-M) (tot*=K)%=mo;
	
	FOR(i,M) {
		ZERO(cnt);
		int ng=0;
		for(j=i;j<M && j<i+K;j++) if(++cnt[A[j]]==2) ng++;
		if(ng) {
			cout<<tot<<endl;
			return;
		}
	}
	
	ZERO(cnt);
	FOR(i,M) cnt[A[i]]++;
	FOR(i,K) if(cnt[i]>1) break;
	
	if(i<K) {
		// same
		assert(0);
	}
	else {
		// no same
		dp[1][K==1][1]=K;
		for(i=2;i<=N;i++) {
			ZERO(dp2);
			for(j=1;j<K;j++) {
				if(j<K) {
					(dp[i+1][j+1>=M][j+1]+=(K-j)*dp[i][0][j])%=mo;
					(dp[i+1][1][j+1]+=(K-j)*dp[i][1][j])%=mo;
				}
				dp2[0][1]+=dp[i][0][j];
				dp2[0][j+1]+=mo-dp[i][0][j];
				dp2[1][1]+=dp[i][1][j];
				dp2[1][j+1]+=mo-dp[i][1][j];
			}
			
			for(j=1;j<=K;j++) {
				dp2[0][j]+=dp2[0][j-1];
				(dp[i+1][0][j]+=dp2[0][j])%=mo;
				dp2[1][j]+=dp2[1][j-1];
				(dp[i+1][1][j]+=dp2[1][j])%=mo;
			}
		}
		ll ret=0;
		for(j=1;j<K;j++) ret+=dp[N+1][1][j];
		ret%=mo;
		for(i=N-M+1;i<=N;i++) ret=ret*i%mo;
		cout<<(tot-ret+mo)%mo<<endl;
		
	}
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}