#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 2000;
const int mod = 1e9 + 7;
int a[N], n, k;
int l[N], r[N], st[N], ps[N];
int prog(int f, int sh, int n) {
  long long l = f + 1ll * sh * (n - 1);
  l += f;
  if (n % 2 == 0)
    n /= 2;
  else if (l % 2 == 0)
    l /= 2;
  l %= mod;
  n %= mod;
  return 1ll * l * n % mod;
}
void add(int &x, int y) {
  x += y;
  if (x >= mod) x -= mod;
}
void sub(int &x, int y) {
  x -= y;
  if (x < 0) x += mod;
}
int calc(int n, int i) {
  if (n < k) return 0;
  int l = 0;
  int r = n + 2;
  long long t = k;
  while (r - l > 0) {
    int mid = (r + l) / 2;
    mid++;
    if (k + 1ll * mid * (k - 1) > n)
      r = mid - 1;
    else
      l = mid;
  }
  int need = l;
  l = 0;
  r = need + 1;
  while (r - l > 0) {
    int mid = (r + l) / 2;
    if (i - k - mid * (k - 1) + 1 >= 1)
      l = mid + 1;
    else
      r = mid;
  }
  int lim1 = l;
  l = 0;
  r = need + 1;
  while (r - l > 0) {
    int mid = (r + l) / 2;
    if (n - k - mid * (k - 1) + 1 >= i)
      l = mid + 1;
    else
      r = mid;
  }
  int lim2 = l;
  int ans = 0;
  add(ans, need + 1);
  add(ans, 1ll * i * lim2 % mod);
  add(ans, prog(n - k - lim2 * (k - 1) + 1, -(k - 1), need - lim2 + 1));
  sub(ans, prog(i - k + 1, -(k - 1), lim1));
  sub(ans, need - lim1 + 1);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int sz = 0;
  for (int i = 1; i <= n; i++) {
    while (sz >= 1 && a[i] > st[sz]) sz--;
    l[i] = ps[sz] + 1;
    sz++;
    st[sz] = a[i];
    ps[sz] = i;
  }
  sz = 0;
  ps[sz] = n + 1;
  for (int i = n; i >= 1; i--) {
    while (sz >= 1 && a[i] >= st[sz]) sz--;
    r[i] = ps[sz] - 1;
    sz++;
    st[sz] = a[i];
    ps[sz] = i;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = ans + 1ll * a[i] * calc(r[i] - l[i] + 1, i - l[i] + 1) % mod;
    if (ans >= mod) ans -= mod;
  }
  cout << ans << "\n";
  return 0;
}

### Prompt

Please create a solution in CPP to 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 N = 1e6 + 2000;
const int mod = 1e9 + 7;
int a[N], n, k;
int l[N], r[N], st[N], ps[N];
int prog(int f, int sh, int n) {
  long long l = f + 1ll * sh * (n - 1);
  l += f;
  if (n % 2 == 0)
    n /= 2;
  else if (l % 2 == 0)
    l /= 2;
  l %= mod;
  n %= mod;
  return 1ll * l * n % mod;
}
void add(int &x, int y) {
  x += y;
  if (x >= mod) x -= mod;
}
void sub(int &x, int y) {
  x -= y;
  if (x < 0) x += mod;
}
int calc(int n, int i) {
  if (n < k) return 0;
  int l = 0;
  int r = n + 2;
  long long t = k;
  while (r - l > 0) {
    int mid = (r + l) / 2;
    mid++;
    if (k + 1ll * mid * (k - 1) > n)
      r = mid - 1;
    else
      l = mid;
  }
  int need = l;
  l = 0;
  r = need + 1;
  while (r - l > 0) {
    int mid = (r + l) / 2;
    if (i - k - mid * (k - 1) + 1 >= 1)
      l = mid + 1;
    else
      r = mid;
  }
  int lim1 = l;
  l = 0;
  r = need + 1;
  while (r - l > 0) {
    int mid = (r + l) / 2;
    if (n - k - mid * (k - 1) + 1 >= i)
      l = mid + 1;
    else
      r = mid;
  }
  int lim2 = l;
  int ans = 0;
  add(ans, need + 1);
  add(ans, 1ll * i * lim2 % mod);
  add(ans, prog(n - k - lim2 * (k - 1) + 1, -(k - 1), need - lim2 + 1));
  sub(ans, prog(i - k + 1, -(k - 1), lim1));
  sub(ans, need - lim1 + 1);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int sz = 0;
  for (int i = 1; i <= n; i++) {
    while (sz >= 1 && a[i] > st[sz]) sz--;
    l[i] = ps[sz] + 1;
    sz++;
    st[sz] = a[i];
    ps[sz] = i;
  }
  sz = 0;
  ps[sz] = n + 1;
  for (int i = n; i >= 1; i--) {
    while (sz >= 1 && a[i] >= st[sz]) sz--;
    r[i] = ps[sz] - 1;
    sz++;
    st[sz] = a[i];
    ps[sz] = i;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = ans + 1ll * a[i] * calc(r[i] - l[i] + 1, i - l[i] + 1) % mod;
    if (ans >= mod) ans -= mod;
  }
  cout << ans << "\n";
  return 0;
}

```