#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
long long int arr[1000010];
int leftPos[1000010], rightPos[1000010];
stack<pair<long long int, int> > mstk;
long long int quasiEuclidean(long long int a, long long int b, long long int c,
                             long long int n) {
  if (a == 0) {
    return (n + 1) * (b / c) % mod;
  }
  if (a >= c || b >= c) {
    long long int tmp =
        ((n & 1) ? ((n + 1) >> 1) * n : (n >> 1) * (n + 1)) % mod;
    return ((a / c) * tmp % mod + (b / c) * (n + 1) % mod +
            quasiEuclidean(a % c, b % c, c, n)) %
           mod;
  }
  long long int m = (a * n + b) / c;
  return (n * m % mod - quasiEuclidean(c, c - b - 1, a, m - 1) + mod) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int len, k;
  cin >> len >> k;
  for (int i = 0; i < len; i++) cin >> arr[i];
  leftPos[0] = 0;
  mstk.push(make_pair(arr[0], 0));
  for (int i = 1; i < len; i++) {
    while (!mstk.empty() && mstk.top().first <= arr[i]) mstk.pop();
    if (mstk.empty())
      leftPos[i] = 0;
    else
      leftPos[i] = mstk.top().second + 1;
    mstk.push(make_pair(arr[i], i));
  }
  mstk = stack<pair<long long int, int> >();
  rightPos[len - 1] = len - 1;
  mstk.push(make_pair(arr[len - 1], len - 1));
  for (int i = len - 2; i >= 0; i--) {
    while (!mstk.empty() && mstk.top().first < arr[i]) mstk.pop();
    if (mstk.empty())
      rightPos[i] = len - 1;
    else
      rightPos[i] = mstk.top().second - 1;
    mstk.push(make_pair(arr[i], i));
  }
  long long int ans = 0;
  for (long long int i = 0; i < len; i++) {
    long long int l = leftPos[i], r = rightPos[i];
    ans += (quasiEuclidean(1, r - i, k - 1, i - l) -
            quasiEuclidean(1, 0, k - 1, i - l - 1) + mod) %
           mod * arr[i] % mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Develop a solution in cpp to the problem described below:

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 long long int mod = 1e9 + 7;
long long int arr[1000010];
int leftPos[1000010], rightPos[1000010];
stack<pair<long long int, int> > mstk;
long long int quasiEuclidean(long long int a, long long int b, long long int c,
                             long long int n) {
  if (a == 0) {
    return (n + 1) * (b / c) % mod;
  }
  if (a >= c || b >= c) {
    long long int tmp =
        ((n & 1) ? ((n + 1) >> 1) * n : (n >> 1) * (n + 1)) % mod;
    return ((a / c) * tmp % mod + (b / c) * (n + 1) % mod +
            quasiEuclidean(a % c, b % c, c, n)) %
           mod;
  }
  long long int m = (a * n + b) / c;
  return (n * m % mod - quasiEuclidean(c, c - b - 1, a, m - 1) + mod) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int len, k;
  cin >> len >> k;
  for (int i = 0; i < len; i++) cin >> arr[i];
  leftPos[0] = 0;
  mstk.push(make_pair(arr[0], 0));
  for (int i = 1; i < len; i++) {
    while (!mstk.empty() && mstk.top().first <= arr[i]) mstk.pop();
    if (mstk.empty())
      leftPos[i] = 0;
    else
      leftPos[i] = mstk.top().second + 1;
    mstk.push(make_pair(arr[i], i));
  }
  mstk = stack<pair<long long int, int> >();
  rightPos[len - 1] = len - 1;
  mstk.push(make_pair(arr[len - 1], len - 1));
  for (int i = len - 2; i >= 0; i--) {
    while (!mstk.empty() && mstk.top().first < arr[i]) mstk.pop();
    if (mstk.empty())
      rightPos[i] = len - 1;
    else
      rightPos[i] = mstk.top().second - 1;
    mstk.push(make_pair(arr[i], i));
  }
  long long int ans = 0;
  for (long long int i = 0; i < len; i++) {
    long long int l = leftPos[i], r = rightPos[i];
    ans += (quasiEuclidean(1, r - i, k - 1, i - l) -
            quasiEuclidean(1, 0, k - 1, i - l - 1) + mod) %
           mod * arr[i] % mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

```