Maximum Reduction

Given an array $a$ of $n$ integers and an integer $k$ ($2 \le k \le n$), where each element of the array is denoted by $a_i$ ($0 \le 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 \le k \le n \le 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, \ldots, a_{n – 1}$ ($1 \le a_{i} \le 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$.

Solution:

#include <bits/stdc++.h>

using namespace std;

const int md = (int) 1e9 + 7;

inline void add(int &a, int b) {
a += b;
if (a >= md) a -= md;
}

inline void sub(int &a, int b) {
a -= b;
if (a < 0) a += md;
}

inline int mul(int a, int b) {
  return (int) ((long long) a * b % md);
}

inline int power(int a, long long b) {
  int res = 1;
  while (b > 0) {
if (b & 1) {
res = mul(res, a);
}
a = mul(a, a);
b >>= 1;
}
return res;
}

inline int inv(int a) {
a %= md;
if (a < 0) a += md;
  int b = md, u = 0, v = 1;
  while (a) {
    int t = b / a;
    b -= t * a; swap(a, b);
    u -= t * v; swap(u, v);
  }
  assert(b == 1);
  if (u < 0) u += md;
  return u;
}

const int INV2 = inv(2);

inline int sum_positive(int start, int finish, int step) {
  if (start <= 0) {
    return 0;
  }
  if (finish < 0) {
    finish += abs(finish) / step * step;
  }
  while (finish < 0) {
    finish += step;
  }
  int cnt = (start - finish) / step + 1;
  int value = mul(mul(start + finish, cnt), INV2);
  return value;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
vector<pair<int,int>> e(n);
for (int i = 0; i < n; i++) {
    cin >> e[i].first;
e[i].second = i;
}
sort(e.begin(), e.end());
vector<int> pr(n), ne(n);
for (int i = 0; i < n; i++) {
    pr[i] = i - 1;
    ne[i] = i + 1;
  }
  const int step = k - 1;
  int ans = 0;
  for (auto &pp : e) {
    int i = pp.second;
    int L = pr[i] + 1;
    int R = ne[i] - 1;
    int len = (R - L + 1);
    if (len >= k) {
int pos = i - L;
int xstart = k;
int xfinish = k + (len - k) / step * step;
int xcnt = (xfinish - xstart) / step + 1;
{
int value = sum_positive(pos + 1 - xstart, pos + 1 - xfinish, step);
sub(ans, mul(value, pp.first));
}
{
add(ans, mul(mul(pos, xcnt), pp.first));
int value = sum_positive(xfinish + pos - len, xstart + pos - len, step);
sub(ans, mul(value, pp.first));
}
add(ans, mul(xcnt, pp.first));
}
if (pr[i] != -1) {
ne[pr[i]] = ne[i];
}
if (ne[i] != n) {
pr[ne[i]] = pr[i];
}
}
cout << ans << '\n';
  return 0;
}