#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
  gn(x1), gn(x2);
}
template <class T1, class T2, class T3>
inline void gn(T1 &x1, T2 &x2, T3 &x3) {
  gn(x1, x2), gn(x3);
}
template <class T1, class T2, class T3, class T4>
inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
  gn(x1, x2, x3), gn(x4);
}
template <class T1, class T2, class T3, class T4, class T5>
inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
  gn(x1, x2, x3, x4), gn(x5);
}
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <class T>
inline void println(T first) {
  print(first);
  putchar('\n');
}
template <class T>
inline void printsp(T first) {
  print(first);
  putchar(' ');
}
template <class T1, class T2>
inline void print(T1 x1, T2 x2) {
  printsp(x1), println(x2);
}
template <class T1, class T2, class T3>
inline void print(T1 x1, T2 x2, T3 x3) {
  printsp(x1), printsp(x2), println(x3);
}
template <class T1, class T2, class T3, class T4>
inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {
  printsp(x1), printsp(x2), printsp(x3), println(x4);
}
template <class T1, class T2, class T3, class T4, class T5>
inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {
  printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
const int inf = 0x3f3f3f3f, mod = 1000000007, MOD = 998244353;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const double pi = 3.141592653589793, eps = 1e-9;
const int NN = 1010101;
int n, k;
int a[NN], b[NN], c[NN], q[NN];
pair<int, int> p[NN];
int B[NN];
void init() {
  for (int i = 0; i < NN; i++) B[i] = 0;
}
void update(int u, int d) {
  for (; u < NN; u += u & -u) B[u] += d;
}
int calc(int u, int ans = 0) {
  if (!u) return 0;
  for (; u > 0; u -= u & -u) ans += B[u];
  return ans;
}
int kim() {
  int st = 0, ed = NN - 1, md;
  while (st + 1 < ed) {
    md = st + ed >> 1;
    if (calc(md) == k)
      ed = md;
    else
      st = md;
  }
  return ed;
}
int d[NN][20], m;
void dinit() {
  for (int i = 0; i < m; i++) d[i][0] = c[i];
  for (int j = 1; j < 20; j++) {
    for (int i = 0; i < m; i++) {
      d[i][j] = max(d[i][j - 1], d[min(m - 1, (i + (1 << j - 1)))][j - 1]);
    }
  }
}
int kk(int st, int ed) {
  int r = 0;
  while ((1 << r) <= ed - st + 1) r++;
  r--;
  return max(d[st][r], d[ed - (1 << r) + 1][r]);
}
int nam(int ans = 0) {
  if (m == 1) return c[0];
  dinit();
  for (int i = 0, mx, mn; i < m; i++) {
    int st = i, ed = m, md;
    while (st + 1 < ed) {
      md = st + ed >> 1;
      if (kk(i, md) <= c[i])
        st = md;
      else
        ed = md;
    }
    mx = st;
    mn = i;
    if (i > 0 && c[i - 1] < c[i]) {
      st = -1, ed = i - 1;
      while (st + 1 < ed) {
        md = st + ed >> 1;
        if (kk(md, i - 1) < c[i])
          ed = md;
        else
          st = md;
      }
      mn = ed;
    }
    ans += (long long)(mx - i + 1) * (i - mn + 1) % mod * c[i] % mod;
    if (ans >= mod) ans -= mod;
  }
  return ans;
}
int main() {
  gn(n, k);
  for (int i = 1; i <= n; i++) gn(a[i]);
  for (int i = 1; i <= n; i++) p[i] = pair<int, int>(a[i], i);
  sort(p + 1, p + n + 1);
  for (int i = 1; i <= n; i++) q[p[i].second] = i;
  for (int i = 1; i <= n; i++) {
    if (i < k) {
      update(q[i], 1);
    } else if (i == k) {
      update(q[i], 1);
      b[i - k] = p[kim()].first;
    } else {
      update(q[i], 1);
      update(q[i - k], -1);
      b[i - k] = p[kim()].first;
    }
    if (i == 0) {
      cerr << b[i - k] << endl;
      cerr << calc(1) << ' ' << calc(2) << ' ' << calc(3) << ' ' << calc(4)
           << ' ' << kim() << endl;
    }
  }
  n -= k - 1;
  long long ans = 0;
  for (int i = 0; i < k - 1; i++) {
    m = 0;
    for (int j = i; j < n; j += k - 1) c[m++] = b[j];
    ans += nam();
    ans %= mod;
  }
  println(ans);
  return 0;
}

### Prompt

Construct a cpp code solution to the problem outlined:

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, typename U>
inline void smin(T &a, const U &b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
  gn(x1), gn(x2);
}
template <class T1, class T2, class T3>
inline void gn(T1 &x1, T2 &x2, T3 &x3) {
  gn(x1, x2), gn(x3);
}
template <class T1, class T2, class T3, class T4>
inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
  gn(x1, x2, x3), gn(x4);
}
template <class T1, class T2, class T3, class T4, class T5>
inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
  gn(x1, x2, x3, x4), gn(x5);
}
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <class T>
inline void println(T first) {
  print(first);
  putchar('\n');
}
template <class T>
inline void printsp(T first) {
  print(first);
  putchar(' ');
}
template <class T1, class T2>
inline void print(T1 x1, T2 x2) {
  printsp(x1), println(x2);
}
template <class T1, class T2, class T3>
inline void print(T1 x1, T2 x2, T3 x3) {
  printsp(x1), printsp(x2), println(x3);
}
template <class T1, class T2, class T3, class T4>
inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {
  printsp(x1), printsp(x2), printsp(x3), println(x4);
}
template <class T1, class T2, class T3, class T4, class T5>
inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {
  printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
const int inf = 0x3f3f3f3f, mod = 1000000007, MOD = 998244353;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const double pi = 3.141592653589793, eps = 1e-9;
const int NN = 1010101;
int n, k;
int a[NN], b[NN], c[NN], q[NN];
pair<int, int> p[NN];
int B[NN];
void init() {
  for (int i = 0; i < NN; i++) B[i] = 0;
}
void update(int u, int d) {
  for (; u < NN; u += u & -u) B[u] += d;
}
int calc(int u, int ans = 0) {
  if (!u) return 0;
  for (; u > 0; u -= u & -u) ans += B[u];
  return ans;
}
int kim() {
  int st = 0, ed = NN - 1, md;
  while (st + 1 < ed) {
    md = st + ed >> 1;
    if (calc(md) == k)
      ed = md;
    else
      st = md;
  }
  return ed;
}
int d[NN][20], m;
void dinit() {
  for (int i = 0; i < m; i++) d[i][0] = c[i];
  for (int j = 1; j < 20; j++) {
    for (int i = 0; i < m; i++) {
      d[i][j] = max(d[i][j - 1], d[min(m - 1, (i + (1 << j - 1)))][j - 1]);
    }
  }
}
int kk(int st, int ed) {
  int r = 0;
  while ((1 << r) <= ed - st + 1) r++;
  r--;
  return max(d[st][r], d[ed - (1 << r) + 1][r]);
}
int nam(int ans = 0) {
  if (m == 1) return c[0];
  dinit();
  for (int i = 0, mx, mn; i < m; i++) {
    int st = i, ed = m, md;
    while (st + 1 < ed) {
      md = st + ed >> 1;
      if (kk(i, md) <= c[i])
        st = md;
      else
        ed = md;
    }
    mx = st;
    mn = i;
    if (i > 0 && c[i - 1] < c[i]) {
      st = -1, ed = i - 1;
      while (st + 1 < ed) {
        md = st + ed >> 1;
        if (kk(md, i - 1) < c[i])
          ed = md;
        else
          st = md;
      }
      mn = ed;
    }
    ans += (long long)(mx - i + 1) * (i - mn + 1) % mod * c[i] % mod;
    if (ans >= mod) ans -= mod;
  }
  return ans;
}
int main() {
  gn(n, k);
  for (int i = 1; i <= n; i++) gn(a[i]);
  for (int i = 1; i <= n; i++) p[i] = pair<int, int>(a[i], i);
  sort(p + 1, p + n + 1);
  for (int i = 1; i <= n; i++) q[p[i].second] = i;
  for (int i = 1; i <= n; i++) {
    if (i < k) {
      update(q[i], 1);
    } else if (i == k) {
      update(q[i], 1);
      b[i - k] = p[kim()].first;
    } else {
      update(q[i], 1);
      update(q[i - k], -1);
      b[i - k] = p[kim()].first;
    }
    if (i == 0) {
      cerr << b[i - k] << endl;
      cerr << calc(1) << ' ' << calc(2) << ' ' << calc(3) << ' ' << calc(4)
           << ' ' << kim() << endl;
    }
  }
  n -= k - 1;
  long long ans = 0;
  for (int i = 0; i < k - 1; i++) {
    m = 0;
    for (int j = i; j < n; j += k - 1) c[m++] = b[j];
    ans += nam();
    ans %= mod;
  }
  println(ans);
  return 0;
}

```