#include <bits/stdc++.h>
using namespace std;
template <typename T>
T getint() {
  T x = 0, p = 1;
  char ch;
  do {
    ch = getchar();
  } while (ch <= ' ');
  if (ch == '-') p = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * p;
}
mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
template <typename T1, typename T2>
bool umin(T1 &x, const T2 &y) {
  if (x > y) return x = y, true;
  return false;
}
template <typename T1, typename T2>
bool umax(T1 &x, const T2 &y) {
  if (x < y) return x = y, true;
  return false;
}
const int maxn = (int)1e6 + 10;
const int inf = (int)1e9 + 5;
const int mod = 1e9 + 7;
const long long llinf = (long long)1e18 + 5;
const long double pi = acos(-1.0);
long long z(vector<int> a, int k) {
  if (a.size() < k) return 0;
  vector<int> b;
  long long ret = 0;
  for (int i = 0; i + k - 1 < a.size(); ++i) {
    int x = 0;
    for (int j = 0; j < k; ++j) {
      x = max(x, a[i + j]);
    }
    b.push_back(x);
    ret += x;
  }
  return ret + z(b, k);
}
long long brute(vector<int> a, int k) {
  long long ret = 0;
  int n = a.size();
  for (int i = 0; i < n; ++i) {
    int x = 0;
    for (int j = i; j < n; ++j) {
      x = max(x, a[j]);
      if (j - i + 1 >= k && (j - i + 1 - k) % (k - 1) == 0) {
        ret += x;
      }
    }
  }
  return ret;
}
int n, k;
int a[maxn];
int res = 0;
int cmp(int i, int j) { return a[i] > a[j] ? i : j; }
int t[2 * maxn];
void build() {
  for (int i = n - 1; i > 0; --i) t[i] = cmp(t[i << 1], t[i << 1 | 1]);
}
void modify(int p, int value) {
  for (t[p += n] = value; p > 1; p >>= 1) t[p >> 1] = cmp(t[p], t[p ^ 1]);
}
int query(int l, int r) {
  r++;
  int res = l;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res = cmp(res, t[l++]);
    if (r & 1) res = cmp(res, t[--r]);
  }
  return res;
}
int calc(int r, int z) {
  if (r < z) return 0;
  return (r - z) / k + 1;
}
int calc(int l, int r, int z) { return calc(r, z) - calc(l - 1, z); }
void go(int l, int r) {
  if (l > r) return;
  int m = query(l, r);
  long long cnt = 0;
  if (r - m < m - l) {
    int rem = m % k;
    for (int j = m; j <= r; ++j) {
      cnt += calc(l, m, rem);
      if (cnt >= mod) cnt -= mod;
      if (rem == k - 1)
        rem = 0;
      else
        rem++;
    }
  } else {
    int rem = l % k;
    for (int i = l; i <= m; ++i) {
      cnt += calc(m, r, rem);
      if (cnt >= mod) cnt -= mod;
      if (rem == k - 1)
        rem = 0;
      else
        rem++;
    }
  }
  res += (cnt * a[m]) % mod;
  if (res >= mod) res -= mod;
  go(l, m - 1);
  go(m + 1, r);
}
int solve() {
  go(0, n - 1);
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    t[n + i] = i;
  }
  build();
  --k;
  int res2 = solve();
  for (int i = 0; i < n; ++i) {
    res2 -= a[i];
    if (res2 < 0) res2 += mod;
  }
  cout << res2 << endl;
  return 0;
}

### Prompt

Develop a solution in cpp 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;
template <typename T>
T getint() {
  T x = 0, p = 1;
  char ch;
  do {
    ch = getchar();
  } while (ch <= ' ');
  if (ch == '-') p = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * p;
}
mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
template <typename T1, typename T2>
bool umin(T1 &x, const T2 &y) {
  if (x > y) return x = y, true;
  return false;
}
template <typename T1, typename T2>
bool umax(T1 &x, const T2 &y) {
  if (x < y) return x = y, true;
  return false;
}
const int maxn = (int)1e6 + 10;
const int inf = (int)1e9 + 5;
const int mod = 1e9 + 7;
const long long llinf = (long long)1e18 + 5;
const long double pi = acos(-1.0);
long long z(vector<int> a, int k) {
  if (a.size() < k) return 0;
  vector<int> b;
  long long ret = 0;
  for (int i = 0; i + k - 1 < a.size(); ++i) {
    int x = 0;
    for (int j = 0; j < k; ++j) {
      x = max(x, a[i + j]);
    }
    b.push_back(x);
    ret += x;
  }
  return ret + z(b, k);
}
long long brute(vector<int> a, int k) {
  long long ret = 0;
  int n = a.size();
  for (int i = 0; i < n; ++i) {
    int x = 0;
    for (int j = i; j < n; ++j) {
      x = max(x, a[j]);
      if (j - i + 1 >= k && (j - i + 1 - k) % (k - 1) == 0) {
        ret += x;
      }
    }
  }
  return ret;
}
int n, k;
int a[maxn];
int res = 0;
int cmp(int i, int j) { return a[i] > a[j] ? i : j; }
int t[2 * maxn];
void build() {
  for (int i = n - 1; i > 0; --i) t[i] = cmp(t[i << 1], t[i << 1 | 1]);
}
void modify(int p, int value) {
  for (t[p += n] = value; p > 1; p >>= 1) t[p >> 1] = cmp(t[p], t[p ^ 1]);
}
int query(int l, int r) {
  r++;
  int res = l;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res = cmp(res, t[l++]);
    if (r & 1) res = cmp(res, t[--r]);
  }
  return res;
}
int calc(int r, int z) {
  if (r < z) return 0;
  return (r - z) / k + 1;
}
int calc(int l, int r, int z) { return calc(r, z) - calc(l - 1, z); }
void go(int l, int r) {
  if (l > r) return;
  int m = query(l, r);
  long long cnt = 0;
  if (r - m < m - l) {
    int rem = m % k;
    for (int j = m; j <= r; ++j) {
      cnt += calc(l, m, rem);
      if (cnt >= mod) cnt -= mod;
      if (rem == k - 1)
        rem = 0;
      else
        rem++;
    }
  } else {
    int rem = l % k;
    for (int i = l; i <= m; ++i) {
      cnt += calc(m, r, rem);
      if (cnt >= mod) cnt -= mod;
      if (rem == k - 1)
        rem = 0;
      else
        rem++;
    }
  }
  res += (cnt * a[m]) % mod;
  if (res >= mod) res -= mod;
  go(l, m - 1);
  go(m + 1, r);
}
int solve() {
  go(0, n - 1);
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    t[n + i] = i;
  }
  build();
  --k;
  int res2 = solve();
  for (int i = 0; i < n; ++i) {
    res2 -= a[i];
    if (res2 < 0) res2 += mod;
  }
  cout << res2 << endl;
  return 0;
}

```