#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long maxn = 1e6 + 222;
long long ori[maxn];
long long L[maxn], R[maxn];
long long k;
inline long long cal1(long long l, long long r) {
  return (1ll * (l / (k - 1) + 1) * (r / (k - 1) + 1) - 1) % mod;
}
inline long long cal(long long l, long long r) {
  long long ans = 0;
  long long p = k - 1;
  long long tot = min(r + 1, k - 2);
  while (tot) {
    long long lmod = l % p;
    long long rmod = r % p;
    long long t = min(p - lmod, rmod + 1);
    t = min(t, tot);
    tot -= t;
    ans += 1ll * t * (l / p) * (r / p + 1) % mod;
    l += t;
    r -= t;
  }
  return ans;
}
stack<pair<long long, long long> > st;
vector<long long> v;
map<long long, vector<long long> > vv;
long long ma[maxn];
void solve() {
  long long n;
  scanf("%lld %lld", &n, &k);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &ori[i]);
    v.push_back(ori[i]);
  }
  sort(v.begin(), v.end());
  for (long long i = 0; i < n; i++) {
    vv[v[i]].push_back(i);
  }
  for (long long i = 1; i <= n; i++) {
    ma[i] = vv[ori[i]].back();
    vv[ori[i]].pop_back();
  }
  for (long long i = 1; i <= n; i++) {
    R[i] = n;
    while (!st.empty() && st.top().first < ma[i]) {
      R[st.top().second] = i - 1;
      st.pop();
    }
    if (st.empty())
      L[i] = 1;
    else
      L[i] = st.top().second + 1;
    st.push(make_pair(ma[i], i));
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long cnt = 0;
    long long llen = i - L[i];
    long long rlen = R[i] - i;
    if (R[i] - i - 1 >= 0) cnt += cal(llen + 1, rlen - 1);
    cnt += cal1(llen, rlen);
    ans += 1ll * cnt * v[ma[i]] % mod;
    ans %= mod;
  }
  cout << ans << endl;
}
signed main() { solve(); }

### 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>
using namespace std;
const long long mod = 1e9 + 7;
const long long maxn = 1e6 + 222;
long long ori[maxn];
long long L[maxn], R[maxn];
long long k;
inline long long cal1(long long l, long long r) {
  return (1ll * (l / (k - 1) + 1) * (r / (k - 1) + 1) - 1) % mod;
}
inline long long cal(long long l, long long r) {
  long long ans = 0;
  long long p = k - 1;
  long long tot = min(r + 1, k - 2);
  while (tot) {
    long long lmod = l % p;
    long long rmod = r % p;
    long long t = min(p - lmod, rmod + 1);
    t = min(t, tot);
    tot -= t;
    ans += 1ll * t * (l / p) * (r / p + 1) % mod;
    l += t;
    r -= t;
  }
  return ans;
}
stack<pair<long long, long long> > st;
vector<long long> v;
map<long long, vector<long long> > vv;
long long ma[maxn];
void solve() {
  long long n;
  scanf("%lld %lld", &n, &k);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &ori[i]);
    v.push_back(ori[i]);
  }
  sort(v.begin(), v.end());
  for (long long i = 0; i < n; i++) {
    vv[v[i]].push_back(i);
  }
  for (long long i = 1; i <= n; i++) {
    ma[i] = vv[ori[i]].back();
    vv[ori[i]].pop_back();
  }
  for (long long i = 1; i <= n; i++) {
    R[i] = n;
    while (!st.empty() && st.top().first < ma[i]) {
      R[st.top().second] = i - 1;
      st.pop();
    }
    if (st.empty())
      L[i] = 1;
    else
      L[i] = st.top().second + 1;
    st.push(make_pair(ma[i], i));
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long cnt = 0;
    long long llen = i - L[i];
    long long rlen = R[i] - i;
    if (R[i] - i - 1 >= 0) cnt += cal(llen + 1, rlen - 1);
    cnt += cal1(llen, rlen);
    ans += 1ll * cnt * v[ma[i]] % mod;
    ans %= mod;
  }
  cout << ans << endl;
}
signed main() { solve(); }

```