Problem:
Score : 1100 points

Problem Statement

You are given integers N, K, and an integer sequence A of length M.

An integer sequence where each element is between 1 and K (inclusive) is said to be colorful when there exists a contiguous subsequence of length K of the sequence that contains one occurrence of each integer between 1 and K (inclusive).

For every colorful integer sequence of length N, count the number of the contiguous subsequences of that sequence which coincide with A, then find the sum of all the counts.
Here, the answer can be extremely large, so find the sum modulo 10^9+7.

Constraints

1 \leq N \leq 25000

1 \leq K \leq 400

1 \leq M \leq N

1 \leq A_i \leq K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K M
A_1 A_2 ... A_M

Output

For every colorful integer sequence of length N, count the number of the contiguous subsequences of that sequence which coincide with A, then print the sum of all the counts modulo 10^9+7.

Sample Input 1

3 2 1
1

Sample Output 1

9

There are six colorful sequences of length 3: (1,1,2), (1,2,1), (1,2,2), (2,1,1), (2,1,2) and (2,2,1).
The numbers of the contiguous subsequences of these sequences that coincide with A=(1) are 2, 2, 1, 2, 1 and 1, respectively.
Thus, the answer is their sum, 9.

Sample Input 2

4 2 2
1 2

Sample Output 2

12

Sample Input 3

7 4 5
1 2 3 1 2

Sample Output 3

17

Sample Input 4

5 4 3
1 1 1

Sample Output 4

0

Sample Input 5

10 3 5
1 1 2 3 3

Sample Output 5

1458

Sample Input 6

25000 400 4
3 7 31 127

Sample Output 6

923966268

Sample Input 7

9954 310 12
267 193 278 294 6 63 86 166 157 193 168 43

Sample Output 7

979180369

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

cat mod = 1000000007;

cat pw(cat a, cat e) {
	if(e <= 0) return 1;
	cat x = pw(a, e/2);
	x = (x * x) % mod;
	if(e&1) x = (x * a) % mod;
	return x;
}

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int N, K, M;
	cin >> M >> K >> N;
	vector<int> A(N);
	for(int i = 0; i < N; i++) {
		cin >> A[i];
		A[i]--;
	}
	vector<cat> pwK(M+1, 1);
	for(int i = 1; i <= M; i++) pwK[i] = pwK[i-1] * K % mod;

	cat ans = pwK[M-N] * (M-N+1) % mod;

	for(int i = 0; i <= N-K; i++) {
		vector<int> occ(K, 0);
		for(int j = 0; j < K; j++) occ[A[i]]++;
		bool ok = true;
		for(int j = 0; j < K; j++) if(occ[A[i]] == 0) ok = false;
		if(ok) {
			cout << ans << "\n";
			return 0;
		}
	}
	
	int coll = 0, colr = 0;
	vector<int> occ(K, 0);
	for(int i = 0; i < N; i++) {
		if(occ[A[i]]++ == 0) coll++;
		else break;
	}
	for(int i = 0; i < K; i++) occ[i] = 0;
	for(int i = 0; i < N; i++) {
		if(occ[A[N-1-i]]++ == 0) colr++;
		else break;
	}

	vector< vector<cat> > cnt0(K+1, vector<cat>(M+1, 0));
	cnt0[0][0] = 1;
	for(int i = 0; i < M; i++) {
		cat sum = 0;
		for(int k = K-1; k >= 1; k--) {
			sum += cnt0[k][i];
			if(sum >= mod) sum -= mod;
			cnt0[k][i+1] += sum;
			if(cnt0[k][i+1] >= mod) cnt0[k][i+1] -= mod;
		}
		for(int j = 0; j < K; j++)
			cnt0[j+1][i+1] = (cnt0[j+1][i+1] + cnt0[j][i] * (K-j)) % mod;
	}

	cat facl = 1, facr = 1;
	for(int i = 1; i <= coll; i++) facl = facl * (K+1-i) % mod;
	for(int i = 1; i <= colr; i++) facr = facr * (K+1-i) % mod;
	cat invl = pw(facl, mod-2), invr = pw(facr, mod-2);
	vector<cat> inv(K+1, 1);
	for(int i = K; i >= 0; i--) {
		cat fac = 1;
		for(int j = 0; j < i; j++) fac = fac * (K-j) % mod;
		inv[i] = pw(fac, mod-2);
	}

	if(coll < N) {
		vector<cat> x(M+1, 0), y(M+1, 0);
		for(int i = 0; i <= M-N+coll; i++) for(int j = coll; j < K; j++)
			x[i-coll] = (x[i-coll] + cnt0[j][i] * invl) % mod;
		for(int i = 0; i <= M-N+colr; i++) for(int j = colr; j < K; j++)
			y[i-colr] = (y[i-colr] + cnt0[j][i] * invr) % mod;
		for(int i = 0; i <= M-N; i++) ans = (ans - x[i] * y[M-N-i]) % mod;
	}

	else {
		vector< vector<cat> > cnt1(K+1, vector<cat>(M+1, 0));
		for(int i = 0; i <= M; i++) for(int j = K-1; j > 0; j--) {
			cnt1[j][i] = cnt0[j][i] + cnt1[j+1][i];
			if(cnt1[j][i] >= mod) cnt1[j][i] -= mod;
		}
		for(int j = N; j < K; j++) for(int i = 0; i <= M-j; i++) {
			cat x = cnt0[j][i+j] * inv[N] % mod * inv[j] % mod;
			ans = (ans - cnt1[j][M-i] * x) % mod;
		}
	}

	ans %= mod;
	if(ans < 0) ans += mod;
	cout << ans << "\n";
	return 0;}

// look at my code
// my code is amazing