Language: C++

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define ld double
#define llu unsigned long long
#define rep(i,x,y)  for(int i=int(x);i<int(y);++i)
#define For(i,x,y)  for(int i=int(x);i<=int(y);++i)
#define FOr(i,x,y)  for(int i=int(x);i>=int(y);--i)
#define pi acos(-1) 
#define mk make_pair
#define pa pair<ll,ll>
#define lf else if
#define IL inline
#define sqr(x)      ((x)*(x))
#define Mul(x,y)    ((x)=1LL*(x)*(y)%mod)
#define E(x)        return writeln(x),0
#define LL          (long long)
#define p(x)        printf("~%lld~\n",LL(x))
#define pp(x,y)     printf("~~%lld %lld~~\n",LL(x),LL(y))
#define ppp(x,y,z)  printf("~~~%lld %lld %lld~~~\n",LL(x),LL(y),LL(z))
#define pppp(a,b,c,d)   printf("~~~%lld %lld %lld %lld\n",LL(a),LL(b),LL(c),LL(d))
#define f_in(x)     freopen(x".in","r",stdin)
#define f_out(x)    freopen(x".out","w",stdout)
#define open(x)     f_in(x),f_out(x)
#define fi first
#define se second
#define GuYue       puts("\nGuYueNa________________________________________________________________________________")
#define y1          fafa____
typedef complex<double> E;
namespace SHENZHEBEI{
#ifdef LOCAL
    struct _{_(){freopen("cf.in","r",stdin);}}_; 
#endif 
#define NEG 1
    static const int GYN=2333333;
    char SZB[GYN],*SS=SZB,*TT=SZB;
    inline char gc(){   if (SS==TT){  TT=(SS=SZB)+fread(SZB,1,GYN,stdin); if (SS==TT) return '\n';  }   return *SS++;    }
#if NEG
    inline ll read(){    ll x=0,g=1; char ch=gc();   for (;!isdigit(ch);ch=gc()) if (ch=='-') g=-1;  for (;isdigit(ch);ch=gc())  x=x*10-48+ch;   return x*g; }
    inline void write(ll x){    if (x<0) putchar('-'),x=-x;  if (x>=10)   write(x/10);    putchar(x%10+'0');  }
#else
    inline ll read(){    ll x=0; char ch=gc();   for (;!isdigit(ch);ch=gc());    for (;isdigit(ch);ch=gc())  x=x*10-48+ch;   return x;   }
    inline void write(ll x){    if (x>=10)   write(x/10);    putchar(x%10+'0');  }
#endif
    inline char readchar(){ char ch=gc();   for(;isspace(ch);ch=gc());  return ch;  }
    inline ll readstr(char *s){ char ch=gc();   int cur=0;  for(;isspace(ch);ch=gc());      for(;!isspace(ch);ch=gc())  s[cur++]=ch;    s[cur]='\0';    return cur; }
    void Print(long long *a,int s,int t){For(i,s,t)printf("%lld ",a[i]);puts("");}
    void Print(int *a,int s,int t){For(i,s,t)printf("%d ",a[i]);puts("");}
    void Print(char *a,int s,int t){For(i,s,t)putchar(a[i]);puts("");}
    void writeln(ll x){write(x),puts("");}
    void Max(ll &x,ll y){x=x<y?y:x;}
    void Min(ll &x,ll y){x=x>y?y:x;}
    ll min(ll x,ll y){return x<y?x:y;}
}using namespace SHENZHEBEI;
const ll N=400010,mod=1e9+7;
ll vis[500];
ll ppow(ll x,ll k){
	ll ans=1;
	for(;k;k>>=1,Mul(x,x))if (k&1)Mul(ans,x);
	return ans;
}
ll has(ll x,ll y){
	if (x<0)return 0;
	return f[x][y];
}
ll any(ll x){
	return ppow(k,x);
}
ll none(ll x,ll y){
	return any(x)-has(x,y); 
}
int main(){
	n=read();k=read();m=read();
	fac[0]=1;For(i,1,k)fac[i]=fac[i-1]*i%mod;
	inv[k]=ppow(fac[k],mod-2);
	FOr(i,k,1)inv[i-1]=inv[i]*i%mod;
	For(i,1,m)a[i]=read();
	For(i,1,m-k+1){
		memset(vis,0,sizeof vis);
		bool frog=1;
		For(j,i,i+k-1){
			frog&=!vis[a[i]];
			vis[a[i]]=1; 
		}
		if (frog)return writeln((n-m+1)*ppow(k,n-m)%mod),0;
	}
	f[0][0]=g[0][0]=1;
	For(i,1,n){
		ll pre=f[i-1][0];
		For(j,1,k){
			Add(pre,f[i-1][j]);
			Add(f[i][j],f[i-1][j-1]*(k-j));
			Add(f[i][j],pre);
		}
	}
	For(i,1,n)FOr(j,k,0)Add(f[i][j],f[i][j+1]);
	memset(vis,0,sizeof vis);
	For(i,1,m)if (vis[a[i]])break;
	else		Left=i;
	memset(vis,0,sizeof vis);
	FOr(i,m,1)if (vis[a[i]])break; 
	else		Rigt=m-i+1;
	For(i,1,n){
		Add(answ,has(i-1,Left)*any(n-m-(i-1))+(non(i-1,Left))*has(n-m-(i-1),Rigt));
		For(pre,1,Left)//	[?????mmmm]mmm
			Add(answ,fac[k-pre]*(non(i-1-(k-pre),(k-pre)))%mod*any(n-m-(i-1)));
		For(suc,1,Rigt)
			Add(answ,fac[k-suc]*non(i-1,(k-pre))%mod*any(n-m-(i-1)-(k-suc))%mod);
	}
	writeln((answ+mod)%mod);
}
/*
4
2*
111	2
110	1
101
100
011	1
010
001
000
??????
(n-m+1)/k^m
111
如果包含了
那么 
*/

Compiler output:
a.cc: In function 'long long int has(long long int, long long int)':
a.cc:65:16: error: 'f' was not declared in this scope
   65 |         return f[x][y];
      |                ^
a.cc: In function 'long long int any(long long int)':
a.cc:68:21: error: 'k' was not declared in this scope
   68 |         return ppow(k,x);
      |                     ^
a.cc: In function 'long long int none(long long int, long long int)':
a.cc:71:16: error: reference to 'any' is ambiguous
   71 |         return any(x)-has(x,y);
      |                ^~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:77,
                 from a.cc:1:
/usr/include/c++/14/any:81:9: note: candidates are: 'class std::any'
   81 |   class any
      |         ^~~
a.cc:67:4: note:                 'long long int any(long long int)'
   67 | ll any(ll x){
      |    ^~~
a.cc: In function 'int main()':
a.cc:74:9: error: 'n' was not declared in this scope
   74 |         n=read();k=read();m=read();
      |         ^
a.cc:74:18: error: 'k' was not declared in this scope
   74 |         n=read();k=read();m=read();
      |                  ^
a.cc:74:27: error: 'm' was not declared in this scope; did you mean 'tm'?
   74 |         n=read();k=read();m=read();
      |                           ^
      |                           tm
a.cc:75:9: error: 'fac' was not declared in this scope
   75 |         fac[0]=1;For(i,1,k)fac[i]=fac[i-1]*i%mod;
      |         ^~~
a.cc:76:9: error: 'inv' was not declared in this scope; did you mean 'int'?
   76 |         inv[k]=ppow(fac[k],mod-2);
      |         ^~~
      |         int
a.cc:78:19: error: 'a' was not declared in this scope
   78 |         For(i,1,m)a[i]=read();
      |                   ^
a.cc:83:36: error: 'a' was not declared in this scope
   83 |                         frog&=!vis[a[i]];
      |                                    ^
a.cc:88:9: error: 'f' was not declared in this scope
   88 |         f[0][0]=g[0][0]=1;
      |         ^
a.cc:88:17: error: 'g' was not declared in this scope
   88 |         f[0][0]=g[0][0]=1;
      |                 ^
a.cc:92:25: error: 'Add' was not declared in this scope
   92 |                         Add(pre,f[i-1][j]);
      |                         ^~~
a.cc:97:29: error: 'Add' was not declared in this scope
   97 |         For(i,1,n)FOr(j,k,0)Add(f[i][j],f[i][j+1]);
      |                             ^~~
a.cc:99:27: error: 'a' was not declared in this scope
   99 |         For(i,1,m)if (vis[a[i]])break;
      |                           ^
a.cc:100:25: error: 'Left' was not declared in this scope
  100 |         else            Left=i;
      |                         ^~~~
a.cc:102:27: error: 'a' was not declared in this scope
  102 |         FOr(i,m,1)if (vis[a[i]])break;
      |                           ^
a.cc:103:25: error: 'Rigt' was not declared in this scope
  103 |         else            Rigt=m-i+1;
      |                         ^~~~
a.cc:105:21: error: 'answ' was not declared in this scope
  105 |                 Add(answ,has(i-1,Left)*any(n-m-(i-1))+(non(i-1,Left))*has(n-m-(i-1),Rigt));
      |                     ^~~~
a.cc:105:34: error: 'Left' was not declared in this scope
  105 |                 Add(answ,has(i-1,Left)*any(n-m-(i-1))+(non(i-1,Left))*has(n-m-(i-1),Rigt));
      |                                  ^~~~
a.cc:105:40: error: reference to 'any' is ambiguous
  105 |                 Add(answ,has(i-1,Left)*any(n-m-(i-1))+(non(i-1,Left))*has(n-m-(i-1),Rigt));
      |                                        ^~~
/usr/include/c++/14/any:81:9: note: candidates are: 'class std::any'
   81 |   class any
      |         ^~~
a.cc:67:4: note:                 'long long int any(long long int)'
   67 | ll any(ll x){
      |    ^~~
a.cc:105:56: error: 'non' was not declared in this scope; did you mean 'none'?
  105 |                 Add(answ,has(i-1,Left)*any(n-m-(i-1))+(non(i-1,Left))*has(n-m-(i-1),Rigt));
      |                                                        ^~~
      |                                                        none
a.cc:105:85: error: 'Rigt' was not declared in this scope
  105 |                 Add(answ,has(i-1,Left)*any(n-m-(i-1))+(non(i-1,Left))*has(n-m-(i-1),Rigt));
      |                                                                                     ^~~~
a.cc:105:17: error: 'Add' was not declared in this scope
  105 |                 Add(answ,has(i-1,Left)*any(n-m-(i-1))+(non(i-1,Left))*has(n-m-(i-1),Rigt));
      |                 ^~~
a.cc:107:76: error: reference to 'any' is ambiguous
  107 |                         Add(answ,fac[k-pre]*(non(i-1-(k-pre),(k-pre)))%mod*any(n-m-(i-1)));
      |                                                                            ^~~
/usr/include/c++/14/any:81:9: note: candidates are: 'class std::any'
   81 |   class any
      |         ^~~
a.cc:67:4: note:                 'long long int any(long long int)'
   67 | ll any(ll x){
      |    ^~~
a.cc:109:56: error: 'pre' was not declared in this scope; did you mean 'pread'?
  109 |                         Add(answ,fac[k-suc]*non(i-1,(k-pre))%mod*any(n-m-(i-1)-(k-suc))%mod);
      |                                                        ^~~
      |                                                        pread
a.cc:109:66: error: reference to 'any' is ambiguous
  109 |                         Add(answ,fac[k-suc]*non(i-1,(k-pre))%mod*any(n-m-(i-1)-(k-suc))%mod);
      |                                                                  ^~~
/usr/include/c++/14/any:81:9: note: candidates are: 'class std::any'
   81 |   class any
      |         ^~~
a.cc:67:4: note:                 'long long int any(long long int)'
   67 | ll any(ll x){
      |    ^~~
a.cc:111:18: error: 'answ' was not declared in this scope
  111 |         writeln((answ+mod)%mod);
      |                  ^~~~