#include <bits/stdc++.h>
const int mod = 1000 * 1000 * 1000 + 7;
const int half = (mod + 1) / 2;
using namespace std;
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
int mult(int a, int b) { return (a * (long long)b) % mod; }
int n, k;
int a[1000005];
int l[1000005];
int r[1000005];
int arifm(int n) { return mult(mult(n, n + 1), half); }
int f(int l, int r) {
  if (l > r) swap(l, r);
  int chlenCnt = l / (k - 1);
  int ans = 0;
  if (chlenCnt != 0) {
    ans = chlenCnt;
    ans = add(ans, mult(k - 1, arifm(chlenCnt)));
  }
  int chlenCnt2 = r / (k - 1) - chlenCnt;
  ans = add(ans, mult(l + 1, chlenCnt2));
  int chlenCnt3 = (l + r) / (k - 1) - chlenCnt - chlenCnt2;
  ans = add(ans, mult(l + r + 1, chlenCnt3));
  ans = add(ans, mult(k - 1, arifm(chlenCnt + chlenCnt2)));
  ans = sub(ans, mult(k - 1, arifm(chlenCnt + chlenCnt2 + chlenCnt3)));
  return ans;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  vector<pair<int, int> > st;
  for (int i = 0; i < n; i++) {
    while (st.size() && st.back().first < a[i]) {
      r[st.back().second] = i - 1;
      st.pop_back();
    }
    st.push_back({a[i], i});
  }
  while (st.size()) {
    r[st.back().second] = n - 1;
    st.pop_back();
  }
  reverse(a, a + n);
  for (int i = 0; i < n; i++) {
    while (st.size() && st.back().first <= a[i]) {
      l[st.back().second] = n - i;
      st.pop_back();
    }
    st.push_back({a[i], n - i - 1});
  }
  while (st.size()) {
    l[st.back().second] = 0;
    st.pop_back();
  }
  reverse(a, a + n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = add(ans, mult(a[i], f(i - l[i], r[i] - i)));
  }
  cout << ans;
}

### Prompt

Please provide a cpp coded solution 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>
const int mod = 1000 * 1000 * 1000 + 7;
const int half = (mod + 1) / 2;
using namespace std;
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
int mult(int a, int b) { return (a * (long long)b) % mod; }
int n, k;
int a[1000005];
int l[1000005];
int r[1000005];
int arifm(int n) { return mult(mult(n, n + 1), half); }
int f(int l, int r) {
  if (l > r) swap(l, r);
  int chlenCnt = l / (k - 1);
  int ans = 0;
  if (chlenCnt != 0) {
    ans = chlenCnt;
    ans = add(ans, mult(k - 1, arifm(chlenCnt)));
  }
  int chlenCnt2 = r / (k - 1) - chlenCnt;
  ans = add(ans, mult(l + 1, chlenCnt2));
  int chlenCnt3 = (l + r) / (k - 1) - chlenCnt - chlenCnt2;
  ans = add(ans, mult(l + r + 1, chlenCnt3));
  ans = add(ans, mult(k - 1, arifm(chlenCnt + chlenCnt2)));
  ans = sub(ans, mult(k - 1, arifm(chlenCnt + chlenCnt2 + chlenCnt3)));
  return ans;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  vector<pair<int, int> > st;
  for (int i = 0; i < n; i++) {
    while (st.size() && st.back().first < a[i]) {
      r[st.back().second] = i - 1;
      st.pop_back();
    }
    st.push_back({a[i], i});
  }
  while (st.size()) {
    r[st.back().second] = n - 1;
    st.pop_back();
  }
  reverse(a, a + n);
  for (int i = 0; i < n; i++) {
    while (st.size() && st.back().first <= a[i]) {
      l[st.back().second] = n - i;
      st.pop_back();
    }
    st.push_back({a[i], n - i - 1});
  }
  while (st.size()) {
    l[st.back().second] = 0;
    st.pop_back();
  }
  reverse(a, a + n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = add(ans, mult(a[i], f(i - l[i], r[i] - i)));
  }
  cout << ans;
}

```