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<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const ll MOD=1e9+7;
ll f[25005][405][2],sf[25005][405][2],res[25005][405][2],sres[25005][405][2];
ll g[25005][405][2],sg[25005][405][2];
ll fact[405],inv[405];
int n,m,k,a[25005];
bool flag[405];
ll quick_pow(ll x,ll a)
{
	ll ans=1;
	while(a)
	{
		if(a&1)ans=ans*x%MOD;
		x=x*x%MOD;
		a>>=1;
	}
	return ans;
}
bool check(int st)
{
	memset(flag,0,sizeof(flag));
	for(int i=1;i<=k;i++)
	{
		if(flag[a[st+i-1]])return 0;
		flag[a[st+i-1]]=1;
	}
	return 1;
}
int main()
{
	scanf("%d%d%d",&n,&k,&m);
	fact[0]=1;
	for(int i=1;i<=k;i++)fact[i]=fact[i-1]*i%MOD;
	inv[k]=quick_pow(fact[k],MOD-2);
	for(int i=k;i>=1;i--)inv[i-1]=inv[i]*i%MOD;
	bool ff=0;
	for(int i=1;i<=m;i++)
	{
		scanf("%d",&a[i]);
		if(flag[a[i]])ff=1;
		flag[a[i]]=1;
	}
	if(ff==0)
	{
		f[1][1][k==1]=k;
		for(int i=1;i<=k;i++)
		{
			if(i>=m)
			{
				res[1][i][1]+=f[1][i][1]*inv[k]%MOD*fact[k-m]%MOD;
				res[1][i][0]+=f[1][i][0]*inv[k]%MOD*fact[k-m]%MOD;
			}
			sf[1][i][1]=sf[1][i-1][1]+f[1][i][1];
			sf[1][i][0]=sf[1][i-1][0]+f[1][i][0];
			sres[1][i][1]=(sres[1][i-1][1]+res[1][i][1])%MOD;
			sres[1][i][0]=(sres[1][i-1][0]+res[1][i][0])%MOD;
		}
		for(int i=2;i<=n;i++)
		{
			for(int j=1;j<=k;j++)
			{
				f[i][j][j==k]+=sf[i-1][k][0]-sf[i-1][j-1][0];
				f[i][j][1]+=sf[i-1][k][1]-sf[i-1][j-1][1];
				f[i][j][j==k]+=f[i-1][j-1][0]*(k-j+1);
				f[i][j][1]+=f[i-1][j-1][1]*(k-j+1);
				f[i][j][0]%=MOD,f[i][j][1]%=MOD;
				sf[i][j][1]=(sf[i][j-1][1]+f[i][j][1])%MOD;
				sf[i][j][0]=(sf[i][j-1][0]+f[i][j][0])%MOD;
				
				res[i][j][j==k]+=sres[i-1][k][0]-sres[i-1][j-1][0];
				res[i][j][1]+=sres[i-1][k][1]-sres[i-1][j-1][1];
				res[i][j][j==k]+=res[i-1][j-1][0]*(k-j+1);
				res[i][j][1]+=res[i-1][j-1][1]*(k-j+1);
				if(j>=m)
				{
					res[i][j][1]+=f[i][j][1]*inv[k]%MOD*fact[k-m]%MOD;
					res[i][j][0]+=f[i][j][0]*inv[k]%MOD*fact[k-m]%MOD;
				}
				res[i][j][0]%=MOD,res[i][j][1]%=MOD;
				sres[i][j][1]=(sres[i][j-1][1]+res[i][j][1])%MOD;
				sres[i][j][0]=(sres[i][j-1][0]+res[i][j][0])%MOD;
			}
		}
		printf("%lld\n",sres[n][k][1]%MOD);
		return 0;
	}
	ff=0;
	for(int st=1;st+k-1<=m;st++)
	{
		if(check(st))
		{
			ff=1;
			break;
		}
	}
	if(ff)
	{
		printf("%lld\n",quick_pow(k,n-m)*(n-m+1)%MOD);
		return 0;
	}
	memset(flag,0,sizeof(flag));
	int x=1;
	while(!flag[a[x]])flag[a[x++]]=1;
	x--;
	f[0][x][0]=1;
	for(int j=1;j<=k;j++)
	{
		sf[0][j][1]=(sf[0][j-1][1]+f[0][j][1])%MOD;
		sf[0][j][0]=(sf[0][j-1][0]+f[0][j][0])%MOD;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=k;j++)
		{
			f[i][j][j==k]+=sf[i-1][k][0]-sf[i-1][j-1][0];
			f[i][j][1]+=sf[i-1][k][1]-sf[i-1][j-1][1];
			f[i][j][j==k]+=f[i-1][j-1][0]*(k-j+1);
			f[i][j][1]+=f[i-1][j-1][1]*(k-j+1);
			f[i][j][0]%=MOD,f[i][j][1]%=MOD;
			sf[i][j][1]=(sf[i][j-1][1]+f[i][j][1])%MOD;
			sf[i][j][0]=(sf[i][j-1][0]+f[i][j][0])%MOD;
		}
	}
	x=m;
	memset(flag,0,sizeof(flag));
	while(!flag[a[x]])flag[a[x--]]=1;
	x++;
	x=m-x+1;
	g[0][x][0]=1;
	for(int j=1;j<=k;j++)
	{
		sg[0][j][1]=(sg[0][j-1][1]+g[0][j][1])%MOD;
		sg[0][j][0]=(sg[0][j-1][0]+g[0][j][0])%MOD;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=k;j++)
		{
			g[i][j][j==k]+=sg[i-1][k][0]-sg[i-1][j-1][0];
			g[i][j][1]+=sg[i-1][k][1]-sg[i-1][j-1][1];
			g[i][j][j==k]+=g[i-1][j-1][0]*(k-j+1);
			g[i][j][1]+=g[i-1][j-1][1]*(k-j+1);
			g[i][j][0]%=MOD,g[i][j][1]%=MOD;
			sg[i][j][1]=(sg[i][j-1][1]+g[i][j][1])%MOD;
			sg[i][j][0]=(sg[i][j-1][0]+g[i][j][0])%MOD;
		}
	}
	ll ans=0;
	for(int i=0;i<=n-m;i++)
		ans+=sf[i][k][0]*sg[n-m-i][k][1]%MOD+sf[i][k][1]*sg[n-m-i][k][0]%MOD+sf[i][k][1]*sg[n-m-i][k][1]%MOD;
	printf("%lld\n",ans%MOD);
	return 0;
}