#include <bits/stdc++.h>
#define endl '\n'
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back
using namespace std;
template < class T, class T2> inline int chkmax( T & x, const T2 & y) { return x < y ? x = y, 1 : 0 ; }
template < class T, class T2> inline int chkmin( T & x, const T2 & y) { return x > y ? x = y, 1 : 0 ; }
const int MAXN = ( 1 << 20 ) ;
const int mod = ( int ) 1e9 + 7 ;
const int bound = ( int ) 1e9 + 1 ;
template < class T>
T pw( T a, int pw)
{
T ret( 1 ) ;
while ( pw)
{
if ( pw & 1 ) ret * = a;
a * = a;
pw >>= 1 ;
}
return ret;
}
template < unsigned mod>
class Modint
{
private :
unsigned x;
public :
Modint( ) { x = 0 ; }
Modint( unsigned _x) { x = _x; }
operator unsigned ( ) { return x; }
Modint operator== ( const Modint& m) const { return x == m.x ; }
Modint operator! = ( const Modint& m) const { return x ! = m.x ; }
Modint operator+ = ( const Modint& m) { x = ( x + m.x >= mod ? x + m.x - mod : x + m.x ) ; return * this ; }
Modint operator- = ( const Modint& m) { x = ( x < m.x ? x - m.x + mod : x - m.x ) ; return * this ; }
Modint operator* = ( const Modint& m) { x = 1ULL * x * m.x % mod; return * this ; }
Modint operator+ = ( const int32_t m) { x = ( x + ( m % mod) >= mod ? x + ( m % mod) - mod : x + ( m % mod) ) ; return * this ; }
Modint operator- = ( const int32_t m) { x = ( x < ( m % mod) ? x - ( m % mod) + mod : x - ( m % mod) ) ; return * this ; }
Modint operator* = ( const int32_t m) { x = 1ULL * x * ( m % mod) % mod; return * this ; }
Modint operator+ = ( const int64_t m) { x = ( x + ( m % mod) >= mod ? x + ( m % mod) - mod : x + ( m % mod) ) ; return * this ; }
Modint operator- = ( const int64_t m) { x = ( x < ( m % mod) ? x - ( m % mod) + mod : x - ( m % mod) ) ; return * this ; }
Modint operator* = ( const int64_t m) { x = 1ULL * x * ( m % mod) % mod; return * this ; }
Modint operator+ ( const Modint& m) const { return Modint( * this ) + = m; }
Modint operator- ( const Modint& m) const { return Modint( * this ) - = m; }
Modint operator* ( const Modint& m) const { return Modint( * this ) * = m; }
Modint operator+ ( const int32_t m) const { return Modint( * this ) + = m; }
Modint operator- ( const int32_t m) const { return Modint( * this ) - = m; }
Modint operator* ( const int32_t m) const { return Modint( * this ) * = m; }
Modint operator+ ( const int64_t m) const { return Modint( * this ) + = m; }
Modint operator- ( const int64_t m) const { return Modint( * this ) - = m; }
Modint operator* ( const int64_t m) const { return Modint( * this ) * = m; }
Modint inv( ) { return pw( Modint( * this ) , mod - 2 ) ; }
} ;
Modint< mod> fact[ MAXN] , ifact[ MAXN] ;
void precompute( )
{
fact[ 0 ] = 1 ;
for ( int i = 1 ; i < MAXN; i++ ) fact[ i] = fact[ i - 1 ] * i;
ifact[ MAXN - 1 ] = fact[ MAXN - 1 ] .inv ( ) ;
for ( int i = MAXN - 2 ; i >= 0 ; i-- ) ifact[ i] = ifact[ i + 1 ] * ( i + 1 ) ;
}
Modint< mod> C( int n, int k)
{
if ( n < k || n < 0 || k < 0 ) return Modint< mod> ( 0 ) ;
return fact[ n] * ifact[ n - k] * ifact[ k] ;
}
int n, k;
int a[ MAXN] ;
pair< int , int > tr[ 4 * MAXN] ;
void init( int l, int r, int idx)
{
if ( l == r)
{
tr[ idx] = { a[ l] , l} ;
return ;
}
int mid = ( l + r) >> 1 ;
init( l, mid, 2 * idx + 1 ) ;
init( mid + 1 , r, 2 * idx + 2 ) ;
tr[ idx] = min( tr[ 2 * idx + 1 ] , tr[ 2 * idx + 2 ] ) ;
}
pair< int , int > query( int ql, int qr, int l, int r, int idx)
{
if ( ql > r || l > qr) return { ( int ) 1e9 + 42 , - 1 } ;
if ( ql <= l && r <= qr) return tr[ idx] ;
int mid = ( l + r) >> 1 ;
return min( query( ql, qr, l, mid, 2 * idx + 1 ) , query( ql, qr, mid + 1 , r, 2 * idx + 2 ) ) ;
}
void read( )
{
cin >> n >> k;
for ( int i = 1 ; i <= n - k + 1 ; i++ )
cin >> a[ i] ;
}
Modint< mod> dp[ MAXN] ;
Modint< mod> solve( int l, int r, int statusL, int statusR, int cnt)
{
int len = r - l + 1 ;
for ( int i = 0 ; i <= len; i++ ) dp[ i] = 0 ;
dp[ statusL] = 1 ;
Modint< mod> sum = dp[ statusL] ;
Modint< mod> B = cnt - 1 ;
Modint< mod> P = pw( B, k) ;
for ( int i = statusL + 1 ; i <= len; i++ )
{
dp[ i] = sum;
sum = sum * B + dp[ i] ;
if ( i >= k) sum - = P * dp[ i - k] ;
}
if ( statusR) return dp[ len] ;
return sum;
}
Modint< mod> rec( int l, int r)
{
int L = query( l, r, 1 , n - k + 1 , 0 ) .second , R = L;
while ( L ! = l && a[ L] == a[ L - 1 ] ) L-- ;
while ( R ! = r && a[ R] == a[ R + 1 ] ) R++ ;
Modint< mod> ret = 1 ;
if ( L ! = l) ret * = rec( l, L - 1 ) ;
if ( R ! = r) ret * = rec( R + 1 , r) ;
if ( L ! = l && R ! = r) ret * = solve( L + k - 1 , R, 1 , 1 , bound - a[ L] ) ;
else if ( L ! = l) ret * = solve( L + k - 1 , R + k - 1 , 1 , 0 , bound - a[ L] ) ;
else if ( R ! = r) ret * = solve( L, R, 0 , 1 , bound - a[ L] ) ;
else ret * = solve( L, R + k - 1 , 0 , 0 , bound - a[ L] ) ;
return ret;
}
void solve( )
{
init( 1 , n - k + 1 , 0 ) ;
cout << rec( 1 , n - k + 1 ) << endl;
}
int main( )
{
freopen ( "tracking2.in" , "r" , stdin ) ;
freopen ( "tracking2.out" , "w" , stdout ) ;
ios_base:: sync_with_stdio ( false ) ;
cin .tie ( NULL ) ;
precompute( ) ;
read( ) ;
solve( ) ;
return 0 ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZW5kbCAnXG4nCgovLyNwcmFnbWEgR0NDIG9wdGltaXplICgiTzMiKQovLyNwcmFnbWEgR0NDIHRhcmdldCAoInNzZTQiK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