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 Rust (1.42.0) (judge verdict: Runtime Error)

use proconio::input;
use std::convert::TryFrom;

// modint {{{
mod modint {
    pub trait ModValue:
        std::ops::Add<Output = Self>
        + std::ops::Sub<Output = Self>
        + std::ops::Mul<Output = Self>
        + std::ops::Div<Output = Self>
        + std::ops::Rem<Output = Self>
        + std::ops::AddAssign
        + std::ops::SubAssign
        + std::ops::MulAssign
        + std::ops::DivAssign
        + std::ops::RemAssign
        + std::ops::Neg
        + std::cmp::Ord
        + Clone
        + Copy
        + Sized
        + std::fmt::Debug
        + std::fmt::Display
    {
    }

    impl<
            Value: std::ops::Add<Output = Self>
                + std::ops::Sub<Output = Self>
                + std::ops::Mul<Output = Self>
                + std::ops::Div<Output = Self>
                + std::ops::Rem<Output = Self>
                + std::ops::AddAssign
                + std::ops::SubAssign
                + std::ops::MulAssign
                + std::ops::DivAssign
                + std::ops::RemAssign
                + std::ops::Neg
                + std::cmp::Ord
                + Clone
                + Copy
                + Sized
                + std::fmt::Debug
                + std::fmt::Display,
        > ModValue for Value
    {
    }

    pub trait Minfo: Clone + Copy + std::fmt::Debug {
        type Value: ModValue;

        fn modulus() -> Self::Value;

        fn zero() -> Self::Value;

        fn one() -> Self::Value;
    }

    #[derive(Clone, Copy, Debug)]
    pub struct Mint<Mod: Minfo>(Mod::Value);

    impl<Mod: Minfo> Mint<Mod> {
        #[inline]
        #[allow(dead_code)]
        fn is_within_the_range(x: Mod::Value) -> bool {
            Mod::zero() <= x && x < Mod::modulus()
        }

        #[inline]
        #[allow(dead_code)]
        fn normalize(x: Mod::Value) -> Mod::Value {
            if Self::is_within_the_range(x) {
                x
            } else {
                let x = x % Mod::modulus();
                if x < Mod::zero() {
                    x + Mod::modulus()
                } else {
                    x
                }
            }
        }

        #[allow(dead_code)]
        pub fn zero() -> Self {
            Self(Mod::zero())
        }

        #[allow(dead_code)]
        pub fn one() -> Self {
            Self(Mod::one())
        }

        #[allow(dead_code)]
        pub fn modulus() -> Mod::Value {
            Mod::modulus()
        }

        #[allow(dead_code)]
        pub fn is_zero(&self) -> bool {
            self.value() == Mod::zero()
        }

        #[allow(dead_code)]
        pub fn from_value(x: Mod::Value) -> Self {
            Self(Self::normalize(x))
        }

        #[allow(dead_code)]
        pub fn value(&self) -> Mod::Value {
            self.0
        }

        #[allow(dead_code)]
        pub fn inv(&self) -> Self {
            if self.is_zero() {
                panic!("inv(0)");
            }
            Self(Self::raw_inv(self.value()))
        }

        #[allow(dead_code)]
        pub fn pow(&self, b: u64) -> Self {
            Self(Self::raw_pow(self.value(), b))
        }

        fn raw_add(x: Mod::Value, y: Mod::Value) -> Mod::Value {
            let z = x + y;
            if Mod::modulus() <= z {
                z - Mod::modulus()
            } else {
                z
            }
        }

        fn raw_sub(x: Mod::Value, y: Mod::Value) -> Mod::Value {
            let z = x - y;
            if z < Mod::zero() {
                z + Mod::modulus()
            } else {
                z
            }
        }

        fn raw_neg(x: Mod::Value) -> Mod::Value {
            if x == Mod::zero() {
                Mod::zero()
            } else {
                Mod::modulus() - x
            }
        }

        fn raw_mul(x: Mod::Value, y: Mod::Value) -> Mod::Value {
            x * y % Mod::modulus()
        }

        fn raw_div(x: Mod::Value, y: Mod::Value) -> Mod::Value {
            Self::raw_mul(x, Self::raw_inv(y))
        }

        #[allow(clippy::many_single_char_names)]
        fn raw_inv(mut x: Mod::Value) -> Mod::Value {
            let mut y = Mod::modulus();
            let mut u = Mod::one();
            let mut v = Mod::zero();
            while x != Mod::zero() {
                let q = y / x;
                y -= x * q;
                v -= u * q;
                std::mem::swap(&mut x, &mut y);
                std::mem::swap(&mut u, &mut v);
            }
            Self::normalize(v)
        }

        fn raw_pow(mut a: Mod::Value, mut b: u64) -> Mod::Value {
            let mut x = Mod::one();
            while 0 < b {
                if b % 2 == 1 {
                    x = Self::raw_mul(x, a);
                }
                a = Self::raw_mul(a, a);
                b /= 2
            }
            x
        }
    }

    macro_rules! impl_biop {
        (
            $(
                $biop_trait: ident::$biop_fn: ident,
                $biop_assign_trait: ident::$biop_assign_fn: ident,
                $biop_impl: ident;
            )*
        ) => {
            $(
                impl<Mod: Minfo> std::ops::$biop_trait for Mint<Mod> {
                    type Output = Self;
                    fn $biop_fn(self, rhs: Self) -> Self::Output {
                        Mint(Self::$biop_impl(self.value(), rhs.value()))
                    }
                }
                impl<Mod: Minfo> std::ops::$biop_assign_trait for Mint<Mod>
                where
                    Self: Copy
                {
                    fn $biop_assign_fn(&mut self, rhs: Self) {
                        use std::ops::$biop_trait;
                        *self = Mint::$biop_fn(*self, rhs)
                    }
                }
            )*
        };
    }

    impl_biop! {
        Add::add, AddAssign::add_assign, raw_add;
        Sub::sub, SubAssign::sub_assign, raw_sub;
        Mul::mul, MulAssign::mul_assign, raw_mul;
        Div::div, DivAssign::div_assign, raw_div;
    }

    impl<Mod: Minfo> std::ops::Neg for Mint<Mod> {
        type Output = Self;
        fn neg(self) -> Self::Output {
            Self(Self::raw_neg(self.value()))
        }
    }

    impl<Mod: Minfo> std::cmp::PartialEq for Mint<Mod> {
        fn eq(&self, other: &Self) -> bool {
            self.value().eq(&other.value())
        }
    }
    impl<Mod: Minfo> std::cmp::Eq for Mint<Mod> {}

    impl<Mod: Minfo> std::fmt::Display for Mint<Mod> {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "{}", self.value())
        }
    }

    impl<Mod: Minfo> std::iter::Sum<Self> for Mint<Mod> {
        fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
            iter.fold(Mint::zero(), |acc, x| acc + x)
        }
    }

    impl<Mod: Minfo> std::iter::Product<Self> for Mint<Mod> {
        fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
            iter.fold(Mint::one(), |acc, x| acc * x)
        }
    }

    #[allow(dead_code)]
    pub struct Factorial<Mod: Minfo> {
        normal: Vec<Mint<Mod>>,
        inverted: Vec<Mint<Mod>>,
    }

    #[allow(dead_code)]
    impl<Mod: Minfo> Factorial<Mod>
    where
        Mod::Value: std::convert::TryFrom<usize, Error = std::num::TryFromIntError>,
    {
        pub fn new(n: usize) -> Self {
            use std::convert::TryFrom;
            let mut normal = vec![Mint::one(); n];
            for i in 1..n {
                normal[i] = normal[i - 1] * Mint::from_value(Mod::Value::try_from(i).unwrap());
            }
            let mut inverted = vec![Mint::one(); n];
            inverted[n - 1] = normal[n - 1].inv();
            for i in (1..n).rev() {
                inverted[i - 1] = inverted[i] * Mint::from_value(Mod::Value::try_from(i).unwrap());
            }
            Self { normal, inverted }
        }
    }

    #[allow(dead_code)]
    impl<Mod: Minfo> Factorial<Mod> {
        pub fn inverted(&self, i: usize) -> Mint<Mod> {
            self.inverted[i]
        }
    }

    #[allow(dead_code)]
    impl<Mod: Minfo, I: std::slice::SliceIndex<[Mint<Mod>]>> std::ops::Index<I> for Factorial<Mod> {
        type Output = I::Output;

        #[inline]
        fn index(&self, index: I) -> &Self::Output {
            &self.normal[index]
        }
    }

    #[allow(dead_code)]
    pub struct DoubleFactorial<Mod: Minfo> {
        normal: Vec<Mint<Mod>>,
        inverted: Vec<Mint<Mod>>,
    }

    #[allow(dead_code)]
    impl<Mod: Minfo> DoubleFactorial<Mod>
    where
        Mod::Value: std::convert::TryFrom<usize, Error = std::num::TryFromIntError>,
    {
        pub fn new(n: usize) -> Self {
            use std::convert::TryFrom;
            let mut normal = vec![Mint::one(); n];
            for i in 2..n {
                normal[i] = normal[i - 2] * Mint::from_value(Mod::Value::try_from(i).unwrap());
            }
            let mut inverted = vec![Mint::one(); n];
            inverted[n - 1] = normal[n - 1].inv();
            inverted[n - 2] = normal[n - 2].inv();
            for i in (2..n).rev() {
                inverted[i - 2] = inverted[i] * Mint::from_value(Mod::Value::try_from(i).unwrap());
            }
            Self { normal, inverted }
        }
    }

    #[allow(dead_code)]
    impl<Mod: Minfo> DoubleFactorial<Mod> {
        pub fn inverted(&self, i: usize) -> Mint<Mod> {
            self.inverted[i]
        }
    }

    #[allow(dead_code)]
    impl<Mod: Minfo, I: std::slice::SliceIndex<[Mint<Mod>]>> std::ops::Index<I>
        for DoubleFactorial<Mod>
    {
        type Output = I::Output;

        #[inline]
        fn index(&self, index: I) -> &Self::Output {
            &self.normal[index]
        }
    }

    #[macro_export]
    macro_rules! define_mint {
        (
            struct $modint: ident ($modinfo: ident ($modvalue: expr; $modty: ty));
        ) => {
            #[allow(dead_code)]
            #[derive(Clone, Copy, Debug)]
            pub struct $modinfo {}
            impl Minfo for $modinfo {
                type Value = $modty;
                fn modulus() -> Self::Value {
                    $modvalue
                }
                fn zero() -> Self::Value {
                    0
                }
                fn one() -> Self::Value {
                    1
                }
            }
            type $modint = Mint<$modinfo>;
        };
    }

    #[allow(dead_code)]
    #[derive(Clone, Copy, Debug)]
    pub struct Minfo1000000007 {}

    impl Minfo for Minfo1000000007 {
        type Value = i64;
        fn modulus() -> Self::Value {
            1_000_000_007
        }
        fn zero() -> Self::Value {
            0
        }
        fn one() -> Self::Value {
            1
        }
    }

    #[allow(dead_code)]
    pub type Mint1000000007 = Mint<Minfo1000000007>;

    #[allow(dead_code)]
    #[derive(Clone, Copy, Debug)]
    pub struct Minfo998244353 {}

    impl Minfo for Minfo998244353 {
        type Value = i64;
        fn modulus() -> Self::Value {
            998_244_353
        }
        fn zero() -> Self::Value {
            0
        }
        fn one() -> Self::Value {
            1
        }
    }

    #[allow(dead_code)]
    pub type Mint998244353 = Mint<Minfo998244353>;
}
// }}}

type Mint = modint::Mint1000000007;
type Minfo = modint::Minfo1000000007;

fn has_unique_subseq_of_len_k(a: &[usize], k: usize) -> bool {
    if a.len() < k {
        return false;
    }
    let mut ckd = vec![0; k];
    a[0..k].iter().for_each(|&x| ckd[x] += 1);
    let mut cnt = ckd.iter().filter(|&&x| 0 != x).count();
    if cnt == k {
        return true;
    }
    for l in 0..=a.len() - k {
        let r = l + k;
        ckd[a[l]] -= 1;
        if ckd[a[l]] == 0 {
            cnt -= 1;
        }
        if ckd[a[r]] == 0 {
            cnt += 1;
        }
        ckd[a[r]] += 1;
        if cnt == k {
            return true;
        }
    }
    false
}

fn main() {
    input!(
        n: usize,
        k: usize,
        m: usize,
        a: [proconio::marker::Usize1; m]
    );

    let all = Mint::from_value((n - m + 1) as i64) * Mint::from_value(k as i64).pow((n - m) as u64);
    println!(
        "{}",
        if has_unique_subseq_of_len_k(&a, k) {
            all
        } else {
            let mut dp = vec![vec![Mint::zero(); k]; n + 1];
            let mut ep = vec![vec![Mint::zero(); k]; n + 1];
            dp[0][0] = Mint::one();
            for i in 0..n {
                let [dp_i, dp_i_plus_one] = <&mut [_; 2]>::try_from(&mut dp[i..i + 2]).unwrap();
                let [ep_i, ep_i_plus_one] = <&mut [_; 2]>::try_from(&mut ep[i..i + 2]).unwrap();
                let mut dp_cum = Mint::zero();
                let mut ep_cum = Mint::zero();
                for j in (1..k).rev() {
                    dp_i_plus_one[j] += dp_i[j - 1] * Mint::from_value((k - (j - 1)) as i64);
                    ep_i_plus_one[j] += ep_i[j - 1] * Mint::from_value((k - (j - 1)) as i64);
                    dp_cum += dp_i[j];
                    ep_cum += ep_i[j];
                    dp_i_plus_one[j] += dp_cum;
                    ep_i_plus_one[j] += ep_cum;
                }
                for j in m..k {
                    ep_i_plus_one[j] += dp_i_plus_one[j];
                }
            }

            let fact = modint::Factorial::<Minfo>::new(k + 1);

            let calc_l = |a: &[usize]| -> usize {
                let mut ckd = vec![false; k];
                for (i, &x) in a.iter().enumerate() {
                    if ckd[x] {
                        return i;
                    }
                    ckd[x] = true;
                }
                m
            };
            let l = calc_l(&a);
            let r = calc_l(&a.iter().copied().rev().collect::<Vec<_>>());

            assert_eq!(l == m, r == m);
            let a_is_unique = l == m;

            let complement = if a_is_unique {
                assert!(m <= k);
                ep[n].iter().copied().sum::<Mint>() * fact[k - m] * fact.inverted(k)
            } else {
                (0..=n - m)
                    .map(|i| {
                        dp[i + l][l..].iter().copied().sum::<Mint>()
                            * fact[k - l]
                            * fact.inverted(k)
                            * dp[n - (i + m) + r][r..].iter().copied().sum::<Mint>()
                            * fact[k - r]
                            * fact.inverted(k)
                    })
                    .sum::<Mint>()
            };
            all - complement
        }
    );
}