#include <bits/stdc++.h>
using namespace std;
int n, k, A[1000005], l[1000005], r[1000005], ans;
int mul(int x, int y) { return (1LL * x * y) % 1000000007; }
void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  stack<int> s;
  s.push(0);
  l[0] = 0;
  for (int i = 1; i < n; i++) {
    while (!s.empty() && A[s.top()] < A[i]) {
      s.pop();
    }
    if (s.empty())
      l[i] = 0;
    else
      l[i] = s.top() + 1;
    s.push(i);
  }
  while (!s.empty()) s.pop();
  s.push(n - 1);
  r[n - 1] = n - 1;
  for (int i = n - 2; i >= 0; --i) {
    while (!s.empty() && A[s.top()] <= A[i]) {
      s.pop();
    }
    if (s.empty())
      r[i] = n - 1;
    else
      r[i] = s.top() - 1;
    s.push(i);
  }
  for (int i = 0; i < n; i++) {
    int t1 = (min(i - l[i], r[i] - i) - k) / (k - 1) + 1;
    if (min(i - l[i], r[i] - i) - k >= 0) {
      int cur;
      if (t1 % 2) {
        cur = mul(A[i], t1);
        cur = mul(cur, (k + 1LL * (t1 - 1) * (k - 1) / 2) % 1000000007);
      } else {
        cur = mul(A[i], t1 / 2);
        cur = mul(cur, (2 * k + 1LL * (t1 - 1) * (k - 1)) % 1000000007);
      }
      ans = (ans + cur) % 1000000007;
    }
    if (i - l[i] + 1 <= k) {
      int t2 = (r[i] - i - k) / (k - 1) + 1;
      if (r[i] - i - k >= 0) {
        int cur = mul(A[i], t2);
        ans = (ans + mul(cur, i - l[i] + 1)) % 1000000007;
      }
    } else {
      int ft = k + ((i - l[i] - 1) / (k - 1)) * (k - 1);
      int t2 = (r[i] - i - ft) / (k - 1) + 1;
      if (r[i] - i - ft >= 0) {
        int cur = mul(A[i], t2);
        ans = (ans + mul(cur, i - l[i] + 1)) % 1000000007;
      }
    }
    if (r[i] - i + 1 <= k) {
      int t3 = (i - l[i] - k) / (k - 1) + 1;
      if (i - l[i] - k >= 0) {
        int cur = mul(A[i], t3);
        ans = (ans + mul(cur, r[i] - i + 1)) % 1000000007;
      }
    } else {
      int ft = k + ((r[i] - i - 1) / (k - 1)) * (k - 1);
      int t3 = (i - l[i] - ft) / (k - 1) + 1;
      if (i - l[i] - ft >= 0) {
        int cur = mul(A[i], t3);
        ans = (ans + mul(cur, r[i] - i + 1)) % 1000000007;
      }
    }
    int d = max(i - l[i] + 1, r[i] - i + 1);
    if (d <= k) {
      int t4 = (r[i] - l[i] + 1 - k) / (k - 1) + 1;
      if (r[i] - l[i] + 1 - k >= 0) {
        int cur;
        if (t4 % 2) {
          cur = (mul(r[i] - l[i] + 2, t4) -
                 mul(t4, (k + 1LL * (t4 - 1) * (k - 1) / 2) % 1000000007) +
                 1000000007) %
                1000000007;
        } else {
          cur = (mul(r[i] - l[i] + 2, t4) -
                 mul(t4 / 2, (2 * k + 1LL * (t4 - 1) * (k - 1)) % 1000000007) +
                 1000000007) %
                1000000007;
        }
        ans = (ans + mul(A[i], cur)) % 1000000007;
      }
    } else {
      int ft = k + ((d - 2) / (k - 1)) * (k - 1);
      int t4 = (r[i] - l[i] + 1 - ft) / (k - 1) + 1;
      if (r[i] - l[i] + 1 - ft >= 0) {
        int cur;
        if (t4 % 2) {
          cur = (mul(r[i] - l[i] + 2, t4) -
                 mul(t4, (ft + 1LL * (t4 - 1) * (k - 1) / 2) % 1000000007) +
                 1000000007) %
                1000000007;
        } else {
          cur = (mul(r[i] - l[i] + 2, t4) -
                 mul(t4 / 2, (2 * ft + 1LL * (t4 - 1) * (k - 1)) % 1000000007) +
                 1000000007) %
                1000000007;
        }
        ans = (ans + mul(A[i], cur)) % 1000000007;
      }
    }
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}

### Prompt

Your challenge is to write a Cpp solution 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;
int n, k, A[1000005], l[1000005], r[1000005], ans;
int mul(int x, int y) { return (1LL * x * y) % 1000000007; }
void solve() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  stack<int> s;
  s.push(0);
  l[0] = 0;
  for (int i = 1; i < n; i++) {
    while (!s.empty() && A[s.top()] < A[i]) {
      s.pop();
    }
    if (s.empty())
      l[i] = 0;
    else
      l[i] = s.top() + 1;
    s.push(i);
  }
  while (!s.empty()) s.pop();
  s.push(n - 1);
  r[n - 1] = n - 1;
  for (int i = n - 2; i >= 0; --i) {
    while (!s.empty() && A[s.top()] <= A[i]) {
      s.pop();
    }
    if (s.empty())
      r[i] = n - 1;
    else
      r[i] = s.top() - 1;
    s.push(i);
  }
  for (int i = 0; i < n; i++) {
    int t1 = (min(i - l[i], r[i] - i) - k) / (k - 1) + 1;
    if (min(i - l[i], r[i] - i) - k >= 0) {
      int cur;
      if (t1 % 2) {
        cur = mul(A[i], t1);
        cur = mul(cur, (k + 1LL * (t1 - 1) * (k - 1) / 2) % 1000000007);
      } else {
        cur = mul(A[i], t1 / 2);
        cur = mul(cur, (2 * k + 1LL * (t1 - 1) * (k - 1)) % 1000000007);
      }
      ans = (ans + cur) % 1000000007;
    }
    if (i - l[i] + 1 <= k) {
      int t2 = (r[i] - i - k) / (k - 1) + 1;
      if (r[i] - i - k >= 0) {
        int cur = mul(A[i], t2);
        ans = (ans + mul(cur, i - l[i] + 1)) % 1000000007;
      }
    } else {
      int ft = k + ((i - l[i] - 1) / (k - 1)) * (k - 1);
      int t2 = (r[i] - i - ft) / (k - 1) + 1;
      if (r[i] - i - ft >= 0) {
        int cur = mul(A[i], t2);
        ans = (ans + mul(cur, i - l[i] + 1)) % 1000000007;
      }
    }
    if (r[i] - i + 1 <= k) {
      int t3 = (i - l[i] - k) / (k - 1) + 1;
      if (i - l[i] - k >= 0) {
        int cur = mul(A[i], t3);
        ans = (ans + mul(cur, r[i] - i + 1)) % 1000000007;
      }
    } else {
      int ft = k + ((r[i] - i - 1) / (k - 1)) * (k - 1);
      int t3 = (i - l[i] - ft) / (k - 1) + 1;
      if (i - l[i] - ft >= 0) {
        int cur = mul(A[i], t3);
        ans = (ans + mul(cur, r[i] - i + 1)) % 1000000007;
      }
    }
    int d = max(i - l[i] + 1, r[i] - i + 1);
    if (d <= k) {
      int t4 = (r[i] - l[i] + 1 - k) / (k - 1) + 1;
      if (r[i] - l[i] + 1 - k >= 0) {
        int cur;
        if (t4 % 2) {
          cur = (mul(r[i] - l[i] + 2, t4) -
                 mul(t4, (k + 1LL * (t4 - 1) * (k - 1) / 2) % 1000000007) +
                 1000000007) %
                1000000007;
        } else {
          cur = (mul(r[i] - l[i] + 2, t4) -
                 mul(t4 / 2, (2 * k + 1LL * (t4 - 1) * (k - 1)) % 1000000007) +
                 1000000007) %
                1000000007;
        }
        ans = (ans + mul(A[i], cur)) % 1000000007;
      }
    } else {
      int ft = k + ((d - 2) / (k - 1)) * (k - 1);
      int t4 = (r[i] - l[i] + 1 - ft) / (k - 1) + 1;
      if (r[i] - l[i] + 1 - ft >= 0) {
        int cur;
        if (t4 % 2) {
          cur = (mul(r[i] - l[i] + 2, t4) -
                 mul(t4, (ft + 1LL * (t4 - 1) * (k - 1) / 2) % 1000000007) +
                 1000000007) %
                1000000007;
        } else {
          cur = (mul(r[i] - l[i] + 2, t4) -
                 mul(t4 / 2, (2 * ft + 1LL * (t4 - 1) * (k - 1)) % 1000000007) +
                 1000000007) %
                1000000007;
        }
        ans = (ans + mul(A[i], cur)) % 1000000007;
      }
    }
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}

```