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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
long long n,k,m,a[25001],b[401],dp1[25001][401],dp2[25001][401],mod=1e9+7;
inline long long read()
{
    long long sum=0,x=1;
    char ch=getchar();
    while (ch<'0'||ch>'9')
	{
        if (ch=='-')
		  x=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9')
	{
        sum=sum*10+ch-'0';
        ch=getchar();
    }
    return sum*x;
}
inline void write(long long x)
{
    if (x<0)
	{
        putchar('-');
        x=-x;
    }
    if (x>9)
      write(x/10);
    putchar(x%10+'0');  
    return;
}
inline long long dfs(long long x,long long y)
{
    long long s=1;
    while (y>0)
    {
        if (y%2==1)
		  s=s*x%mod;
        x=x*x%mod;
		y/=2;
    }
    return s;
}
inline long long sr(long long n)
{
    long long s=1;
    for (register long long i=1;i<=n;++i)
	  s=s*i%mod;
    return s;
}
int main()
{
    n=read();m=read();k=read();
    for (register long long i=1;i<=m;i++)
	  a[i]=read();
    long long mx=0,xx=0,ans=(n-m+1)*dfs(k,n-m)%mod;
    for (register long long i=1;i<=m;++i)
    {
        xx=min(xx+1,i-b[a[i]]);
        b[a[i]]=i;
		mx=max(mx,xx);
    }
    if (mx==k)
	{
	    write(ans);
		return 0;
	}
    if (mx==m)
    {
        dp1[0][0]=1;
        for (register long long i=1;i<=n;++i)
        {
            long long s=0,t=0;
            for (register long long j=k-1;j>0;--j)
            {
                s=(s+dp1[i-1][j])%mod;
                t=(t+dp2[i-1][j])%mod;
                dp1[i][j]=(dp1[i][j]+dp1[i-1][j-1]*(k-j+1)%mod)%mod;
                dp2[i][j]=(dp2[i][j]+dp2[i-1][j-1]*(k-j+1)%mod)%mod;
                dp1[i][j]=(dp1[i][j]+s)%mod;
                dp2[i][j]=(dp2[i][j]+t)%mod;
                if (j>=m)
				  dp2[i][j]=(dp2[i][j]+dp1[i][j])%mod;
            }
        }
        long long w=0;
        for (register long long i=1;i<=k;++i)
		  w=(w+dp2[n][i])%mod;
        w=w*sr(k-m)%mod*dfs(sr(k),mod-2)%mod;
        write((ans-w+mod)%mod);
    }else
    {
        long long u=0,v=0;
        memset(b,0,sizeof(b));
        for (register long long i=1;i<=m;++i)
          if (!b[a[i]])
		  {
			++u;
			b[a[i]]=1;
		  }else
		    break;
        memset(b,0,sizeof(b));
        for (register long long i=m;i>0;--i)
          if (!b[a[i]])
		  {
			++v;
			b[a[i]]=1;
		  }else
		    break;
        dp1[0][u]=dp2[0][v]=1;
        for (register long long i=1;i<=n;++i)
        {
            long long s=0,t=0;
            for (register long long j=k-1;j>0;--j)
            {
                s=(s+dp1[i-1][j])%mod;
                t=(t+dp2[i-1][j])%mod;
                dp1[i][j]=(dp1[i][j]+dp1[i-1][j-1]*(k-j+1)%mod)%mod;
                dp2[i][j]=(dp2[i][j]+dp2[i-1][j-1]*(k-j+1)%mod)%mod;
                dp1[i][j]=(dp1[i][j]+s)%mod;
                dp2[i][j]=(dp2[i][j]+t)%mod;
            }
        }
        for (register long long i=0;i+m<=n;++i)
        {
            long long s=0,t=0;
            for (register long long j=1;j<k;++j)
		    {
			    s=(s+dp1[i][j])%mod;
			    t=(t+dp2[n-m-i][j])%mod;
		    }
            ans=(ans+mod-s*t%mod)%mod;
        }
        write(ans);
    }
    return 0;
}