#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long mod2 = 998244353;
long long mod3 = 1000003;
long long mod4 = 998244853;
long long mod5 = 1000000009;
long long inf = 1LL << 60;
double pi = 3.141592653589793238462643383279L;
double eps = 1e-14;
int dh[4] = {1, -1, 0, 0};
int dw[4] = {0, 0, 1, -1};
int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  if (b == 0) return a;
  if (a % b == 0) return b;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  long long c = gcd(a, b);
  return a * b / c;
}
long long Pow(long long n, long long k) {
  long long ret = 1;
  long long now = n;
  while (k > 0) {
    if (k & 1) ret *= now;
    now *= now;
    k /= 2;
  }
  return ret;
}
long long beki(long long n, long long k, long long md) {
  long long ret = 1;
  long long now = n;
  now %= md;
  while (k > 0) {
    if (k % 2 == 1) {
      ret *= now;
      ret %= md;
    }
    now *= now;
    now %= md;
    k /= 2;
  }
  return ret;
}
long long gyaku(long long n, long long md) { return beki(n, md - 2, md); }
long long popcount(long long n) {
  long long ret = 0;
  long long u = n;
  while (u > 0) {
    ret += u % 2;
    u /= 2;
  }
  return ret;
}
long long dfs(vector<long long> v, long long k) {
  if (v.size() < k) return 0;
  long long ret = 0;
  vector<long long> w;
  for (long long i = 0; i < v.size() - k + 1; i++) {
    long long ma = 0;
    for (long long j = i; j < i + k; j++) ma = max(ma, v[j]);
    w.push_back(ma);
    (ret += ma) %= mod;
  }
  (ret += dfs(w, k)) %= mod;
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, k;
  cin >> n >> k;
  k--;
  vector<long long> a(n);
  vector<pair<long long, long long> > v;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    v.push_back({a[i], i});
  }
  set<int> st, sty;
  st.insert(-1);
  sty.insert(-1);
  sort(v.begin(), v.end(), greater<pair<int, int> >());
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long num = v[i].second;
    auto itr = st.lower_bound(num);
    itr--;
    auto itry = sty.lower_bound(n - 1 - num);
    itry--;
    long long sx = num - *itr;
    long long sy = n - 1 - num - *itry;
    long long syou = (sx + k - 1) / k;
    long long r = sx % k;
    if (r == 0) {
      r = k;
    }
    long long syouy = sy / k;
    long long ry = sy % k;
    long long sum = syouy * (syou * r + (k - r) * (syou - 1)) % mod;
    if (ry == 1)
      (sum += syou) %= mod;
    else if (ry >= 2 && ry <= 1 + k - r) {
      (sum += (syou - 1) * ry + 1) %= mod;
    } else if (ry > 1 + k - r) {
      (sum += ry * syou - (k - r)) %= mod;
    }
    (ans += sum * v[i].first) %= mod;
    st.insert(num);
    sty.insert(n - 1 - num);
  }
  long long summ = 0;
  for (long long i = 0; i < n; i++) (summ += a[i]) %= mod;
  ans += -summ + mod;
  cout << ans % mod << endl;
}

### 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;
long long mod = 1000000007;
long long mod2 = 998244353;
long long mod3 = 1000003;
long long mod4 = 998244853;
long long mod5 = 1000000009;
long long inf = 1LL << 60;
double pi = 3.141592653589793238462643383279L;
double eps = 1e-14;
int dh[4] = {1, -1, 0, 0};
int dw[4] = {0, 0, 1, -1};
int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  if (b == 0) return a;
  if (a % b == 0) return b;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  long long c = gcd(a, b);
  return a * b / c;
}
long long Pow(long long n, long long k) {
  long long ret = 1;
  long long now = n;
  while (k > 0) {
    if (k & 1) ret *= now;
    now *= now;
    k /= 2;
  }
  return ret;
}
long long beki(long long n, long long k, long long md) {
  long long ret = 1;
  long long now = n;
  now %= md;
  while (k > 0) {
    if (k % 2 == 1) {
      ret *= now;
      ret %= md;
    }
    now *= now;
    now %= md;
    k /= 2;
  }
  return ret;
}
long long gyaku(long long n, long long md) { return beki(n, md - 2, md); }
long long popcount(long long n) {
  long long ret = 0;
  long long u = n;
  while (u > 0) {
    ret += u % 2;
    u /= 2;
  }
  return ret;
}
long long dfs(vector<long long> v, long long k) {
  if (v.size() < k) return 0;
  long long ret = 0;
  vector<long long> w;
  for (long long i = 0; i < v.size() - k + 1; i++) {
    long long ma = 0;
    for (long long j = i; j < i + k; j++) ma = max(ma, v[j]);
    w.push_back(ma);
    (ret += ma) %= mod;
  }
  (ret += dfs(w, k)) %= mod;
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, k;
  cin >> n >> k;
  k--;
  vector<long long> a(n);
  vector<pair<long long, long long> > v;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    v.push_back({a[i], i});
  }
  set<int> st, sty;
  st.insert(-1);
  sty.insert(-1);
  sort(v.begin(), v.end(), greater<pair<int, int> >());
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long num = v[i].second;
    auto itr = st.lower_bound(num);
    itr--;
    auto itry = sty.lower_bound(n - 1 - num);
    itry--;
    long long sx = num - *itr;
    long long sy = n - 1 - num - *itry;
    long long syou = (sx + k - 1) / k;
    long long r = sx % k;
    if (r == 0) {
      r = k;
    }
    long long syouy = sy / k;
    long long ry = sy % k;
    long long sum = syouy * (syou * r + (k - r) * (syou - 1)) % mod;
    if (ry == 1)
      (sum += syou) %= mod;
    else if (ry >= 2 && ry <= 1 + k - r) {
      (sum += (syou - 1) * ry + 1) %= mod;
    } else if (ry > 1 + k - r) {
      (sum += ry * syou - (k - r)) %= mod;
    }
    (ans += sum * v[i].first) %= mod;
    st.insert(num);
    sty.insert(n - 1 - num);
  }
  long long summ = 0;
  for (long long i = 0; i < n; i++) (summ += a[i]) %= mod;
  ans += -summ + mod;
  cout << ans % mod << endl;
}

```