#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int N, K;
int Count[1000005];
int A[1000005];
int Left[1000005], Right[1000005], Stack[1000005], top;
void Read() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
}
void precalcLeft() {
  for (int i = 1; i <= N; i++) Left[i] = 0;
  for (int i = N; i >= 1; i--) {
    while (top > 0 && A[Stack[top]] < A[i]) Left[Stack[top]] = i, --top;
    Stack[++top] = i;
  }
}
void precalcRight() {
  top = 0;
  for (int i = 1; i <= N; i++) Right[i] = N + 1;
  for (int i = 1; i <= N; i++) {
    while (top > 0 && A[Stack[top]] <= A[i]) Right[Stack[top]] = i, --top;
    Stack[++top] = i;
  }
}
inline void Add(int& x, int y) {
  x += y;
  if (x >= MOD) x -= MOD;
}
void precalcCount() {
  int len = K, cnt = 0;
  int sum = 0;
  for (int i = 1; i <= N; i++) {
    while (len <= i) {
      cnt++;
      Add(sum, len);
      len += K - 1;
    }
    Count[i] = (1LL * (i + 1) * cnt) % MOD;
    Add(Count[i], MOD - sum);
  }
}
void Solve() {
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    int total = Count[Right[i] - Left[i] - 1];
    Add(total, MOD - Count[Right[i] - i - 1]);
    Add(total, MOD - Count[i - Left[i] - 1]);
    Add(ans, (1LL * total * A[i]) % MOD);
  }
  printf("%d\n", ans);
}
int main() {
  Read();
  precalcLeft();
  precalcRight();
  precalcCount();
  Solve();
  return 0;
}

### Prompt

Create a solution in CPP for the following problem:

Given an array a of n integers and an integer k (2 ≤ k ≤ n), where each element of the array is denoted by a_i (0 ≤ i < n). Perform the operation z given below on a and print the value of z(a,k) modulo 10^{9}+7.
    
    
      
    function z(array a, integer k):  
        if length(a) < k:  
            return 0  
        else:  
            b = empty array  
            ans = 0  
            for i = 0 .. (length(a) - k):  
                temp = a[i]  
                for j = i .. (i + k - 1):  
                    temp = max(temp, a[j])  
                append temp to the end of b  
                ans = ans + temp  
            return ans + z(b, k)  
    

Input

The first line of input contains two integers n and k (2 ≤ k ≤ n ≤ 10^6) — the length of the initial array a and the parameter k.

The second line of input contains n integers a_0, a_1, …, a_{n - 1} (1 ≤ a_{i} ≤ 10^9) — the elements of the array a.

Output

Output the only integer, the value of z(a,k) modulo 10^9+7.

Examples

Input

3 2
9 1 10


Output

29


Input

5 3
5 8 7 1 9


Output

34

Note

In the first example: 

  * for a=(9,1,10), ans=19 and b=(9,10), 
  * for a=(9,10), ans=10 and b=(10), 
  * for a=(10), ans=0. 



So the returned value is 19+10+0=29.

In the second example: 

  * for a=(5,8,7,1,9), ans=25 and b=(8,8,9), 
  * for a=(8,8,9), ans=9 and b=(9), 
  * for a=(9), ans=0. 



So the returned value is 25+9+0=34.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int N, K;
int Count[1000005];
int A[1000005];
int Left[1000005], Right[1000005], Stack[1000005], top;
void Read() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
}
void precalcLeft() {
  for (int i = 1; i <= N; i++) Left[i] = 0;
  for (int i = N; i >= 1; i--) {
    while (top > 0 && A[Stack[top]] < A[i]) Left[Stack[top]] = i, --top;
    Stack[++top] = i;
  }
}
void precalcRight() {
  top = 0;
  for (int i = 1; i <= N; i++) Right[i] = N + 1;
  for (int i = 1; i <= N; i++) {
    while (top > 0 && A[Stack[top]] <= A[i]) Right[Stack[top]] = i, --top;
    Stack[++top] = i;
  }
}
inline void Add(int& x, int y) {
  x += y;
  if (x >= MOD) x -= MOD;
}
void precalcCount() {
  int len = K, cnt = 0;
  int sum = 0;
  for (int i = 1; i <= N; i++) {
    while (len <= i) {
      cnt++;
      Add(sum, len);
      len += K - 1;
    }
    Count[i] = (1LL * (i + 1) * cnt) % MOD;
    Add(Count[i], MOD - sum);
  }
}
void Solve() {
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    int total = Count[Right[i] - Left[i] - 1];
    Add(total, MOD - Count[Right[i] - i - 1]);
    Add(total, MOD - Count[i - Left[i] - 1]);
    Add(ans, (1LL * total * A[i]) % MOD);
  }
  printf("%d\n", ans);
}
int main() {
  Read();
  precalcLeft();
  precalcRight();
  precalcCount();
  Solve();
  return 0;
}

```