#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long md = (1e9) + 7;
const int inf = 1 << 30;
long long mmax(long long x, long long y) { return x > y ? x : y; }
int n, k, lg[N], lx[N], rx[N], b[N], _cnt;
long long a[N], ans = 0, sum[N], t[N], t1[N];
void ad(long long& x, long long y) {
  x += y;
  x %= md;
}
inline long long f1(int l, int r) {
  if (l > r || r - l + 1 < k) return 0;
  int cnt = (r - l + 1 - k) / (k - 1);
  return (((long long)(r - l + 2) * (long long)(cnt + 1)) % md - sum[cnt] +
          md) %
         md;
}
inline long long f(int l, int r, int i) {
  return f1(l, r) - f1(l, i - 1) - f1(i + 1, r);
}
int seg[N << 2];
inline void pushup(int rt) { seg[rt] = max(seg[rt << 1], seg[rt << 1 | 1]); }
inline void motify(int rt, int l, int r, int k, int d) {
  if (l == r) {
    seg[rt] = d;
    return;
  }
  int mid = l + r >> 1;
  if (mid >= k)
    motify(rt << 1, l, mid, k, d);
  else
    motify(rt << 1 | 1, mid + 1, r, k, d);
  pushup(rt);
}
inline int query(int rt, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) return seg[rt];
  int mid = l + r >> 1;
  int res = -inf;
  if (mid >= ql) res = max(res, query(rt << 1, l, mid, ql, qr));
  if (mid + 1 <= qr) res = max(res, query(rt << 1 | 1, mid + 1, r, ql, qr));
  return res;
}
int bia(long long x) {
  int l = 1, r = t1[0];
  while (l < r - 1) {
    int mid = l + r >> 1;
    if (t1[mid] < x)
      l = mid + 1;
    else
      r = mid;
  }
  if (t1[l] == x)
    return l;
  else
    return r;
}
void ini(int x) {
  int i;
  for (i = 0; i <= (4e6) + 10; ++i) seg[i] = x;
}
int main() {
  scanf("%d%d", &n, &k);
  register int i, j;
  for (i = 1; i <= n; ++i) scanf("%I64d", &a[i]), t[i] = a[i];
  lg[1] = 0;
  sum[0] = k;
  for (i = 1; i <= n; ++i)
    sum[i] = (sum[i - 1] + (long long)k +
              ((long long)(i) * (long long)(k - 1)) % md) %
             md;
  sort(t + 1, t + 1 + n);
  t[0] = -inf;
  for (i = 1; i <= n; ++i)
    if (t[i] != t[i - 1]) t1[++t1[0]] = t[i];
  for (i = 1; i <= n; ++i) b[i] = bia(a[i]);
  ini(-(n + 1));
  _cnt = t1[0];
  for (i = n; i >= 1; --i) {
    if (b[i] + 1 <= _cnt)
      rx[i] = -query(1, 1, _cnt, b[i] + 1, _cnt) - 1;
    else
      rx[i] = n;
    motify(1, 1, _cnt, b[i], -i);
  }
  ini(0);
  for (i = 1; i <= n; ++i) {
    lx[i] = query(1, 1, _cnt, b[i], _cnt) + 1;
    motify(1, 1, _cnt, b[i], i);
  }
  for (i = 1; i <= n; ++i) ad(ans, (f(lx[i], rx[i], i) * a[i]));
  cout << ans;
}

### 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 + 10;
const long long md = (1e9) + 7;
const int inf = 1 << 30;
long long mmax(long long x, long long y) { return x > y ? x : y; }
int n, k, lg[N], lx[N], rx[N], b[N], _cnt;
long long a[N], ans = 0, sum[N], t[N], t1[N];
void ad(long long& x, long long y) {
  x += y;
  x %= md;
}
inline long long f1(int l, int r) {
  if (l > r || r - l + 1 < k) return 0;
  int cnt = (r - l + 1 - k) / (k - 1);
  return (((long long)(r - l + 2) * (long long)(cnt + 1)) % md - sum[cnt] +
          md) %
         md;
}
inline long long f(int l, int r, int i) {
  return f1(l, r) - f1(l, i - 1) - f1(i + 1, r);
}
int seg[N << 2];
inline void pushup(int rt) { seg[rt] = max(seg[rt << 1], seg[rt << 1 | 1]); }
inline void motify(int rt, int l, int r, int k, int d) {
  if (l == r) {
    seg[rt] = d;
    return;
  }
  int mid = l + r >> 1;
  if (mid >= k)
    motify(rt << 1, l, mid, k, d);
  else
    motify(rt << 1 | 1, mid + 1, r, k, d);
  pushup(rt);
}
inline int query(int rt, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) return seg[rt];
  int mid = l + r >> 1;
  int res = -inf;
  if (mid >= ql) res = max(res, query(rt << 1, l, mid, ql, qr));
  if (mid + 1 <= qr) res = max(res, query(rt << 1 | 1, mid + 1, r, ql, qr));
  return res;
}
int bia(long long x) {
  int l = 1, r = t1[0];
  while (l < r - 1) {
    int mid = l + r >> 1;
    if (t1[mid] < x)
      l = mid + 1;
    else
      r = mid;
  }
  if (t1[l] == x)
    return l;
  else
    return r;
}
void ini(int x) {
  int i;
  for (i = 0; i <= (4e6) + 10; ++i) seg[i] = x;
}
int main() {
  scanf("%d%d", &n, &k);
  register int i, j;
  for (i = 1; i <= n; ++i) scanf("%I64d", &a[i]), t[i] = a[i];
  lg[1] = 0;
  sum[0] = k;
  for (i = 1; i <= n; ++i)
    sum[i] = (sum[i - 1] + (long long)k +
              ((long long)(i) * (long long)(k - 1)) % md) %
             md;
  sort(t + 1, t + 1 + n);
  t[0] = -inf;
  for (i = 1; i <= n; ++i)
    if (t[i] != t[i - 1]) t1[++t1[0]] = t[i];
  for (i = 1; i <= n; ++i) b[i] = bia(a[i]);
  ini(-(n + 1));
  _cnt = t1[0];
  for (i = n; i >= 1; --i) {
    if (b[i] + 1 <= _cnt)
      rx[i] = -query(1, 1, _cnt, b[i] + 1, _cnt) - 1;
    else
      rx[i] = n;
    motify(1, 1, _cnt, b[i], -i);
  }
  ini(0);
  for (i = 1; i <= n; ++i) {
    lx[i] = query(1, 1, _cnt, b[i], _cnt) + 1;
    motify(1, 1, _cnt, b[i], i);
  }
  for (i = 1; i <= n; ++i) ad(ans, (f(lx[i], rx[i], i) * a[i]));
  cout << ans;
}

```