#include <bits/stdc++.h>
using ul = std::uint32_t;
using ull = std::uint64_t;
using li = std::int32_t;
using ll = std::int64_t;
using vul = std::vector<ul>;
const ul base = 1e9 + 7;
const ul sz = 1 << 20;
void insert(ul pos, ul v, vul& tree) {
  for (tree[pos += sz] = v, pos >>= 1; pos; pos >>= 1) {
    tree[pos] = std::max(tree[pos << 1], tree[pos << 1 | 1]);
  }
}
ul query(ul l, ul r, const vul& tree) {
  ul ret = 0;
  for (l = l + sz - 1, r = r + sz; l ^ r ^ 1; l >>= 1, r >>= 1) {
    if (~l & 1) {
      ret = std::max(ret, tree[l ^ 1]);
    }
    if (r & 1) {
      ret = std::max(ret, tree[r ^ 1]);
    }
  }
  return ret;
}
ul plus(ul a, ul b) { return a + b < base ? a + b : a + b - base; }
ul minus(ul a, ul b) { return a < b ? a + base - b : a - b; }
ul times(ul a, ul b) { return ull(a) * b % base; }
class scaler {
 public:
  ul v = 0;
  scaler(ul x) : v(x) {}
  scaler() = default;
};
const scaler twoinv(5e8 + 4);
scaler operator+(const scaler& a, const scaler& b) { return plus(a.v, b.v); }
scaler& operator+=(scaler& a, const scaler& b) {
  a.v = plus(a.v, b.v);
  return a;
}
scaler operator-(const scaler& a, const scaler& b) { return minus(a.v, b.v); }
scaler& operator-=(scaler& a, const scaler& b) {
  a.v = minus(a.v, b.v);
  return a;
}
scaler operator*(const scaler& a, const scaler& b) { return times(a.v, b.v); }
scaler& operator*=(scaler& a, const scaler& b) {
  a.v = times(a.v, b.v);
  return a;
}
li upper(li a, li b) {
  li temp = a / b;
  return temp * b < a ? temp + li(1) : temp;
}
vul tree1(1 << 21, 0), tree2(1 << 21, 0);
ul n, k;
vul datas(1e6 + 1);
vul lws(1e6 + 1);
vul rws(1e6 + 1);
vul datascopy(1e6 + 1);
vul datascopy2(1e6 + 1);
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  std::cin >> n >> k;
  for (ul i = 1; i <= n; ++i) {
    std::cin >> datas[i];
  }
  datascopy = datas;
  datascopy2 = datas;
  std::sort(datascopy.begin() + 1, datascopy.begin() + n + 1);
  for (ul i = 1; i <= n; ++i) {
    datas[i] = std::lower_bound(datascopy.begin() + 1,
                                datascopy.begin() + n + 1, datas[i]) -
               datascopy.begin();
  }
  for (ul i = 1; i <= n; ++i) {
    lws[i] = query(datas[i], sz, tree1) + 1;
    insert(datas[i], i, tree1);
  }
  for (ul i = n; i; --i) {
    rws[i] = n + 1 - query(datas[i] + 1, sz, tree2);
    insert(datas[i], n + 1 - i, tree2);
  }
  scaler ans;
  for (ul i = 1; i <= n; ++i) {
    ul curr = datascopy2[i];
    ul lw = lws[i];
    ul rw = rws[i];
    li a = std::max(upper(li(i - lw), k - 1) - li(1), li(0));
    li b = std::max(upper(li(rw - i) - 1, k - 1) - li(1), li(0));
    li c = (li(rw - lw) - 1) / li(k - 1);
    if (a <= b) {
      ans += (scaler(a) + scaler(a + 1) * scaler(a) * twoinv * scaler(k - 1)) *
             scaler(curr);
      ans += scaler(b - a) * scaler(i + 1 - lw) * scaler(curr);
      ans += (scaler(rw - lw) * scaler(c - b) -
              (scaler(c) * scaler(c + 1) - scaler(b) * scaler(b + 1)) * twoinv *
                  scaler(k - 1)) *
             scaler(curr);
    }
    if (b < a) {
      ans += (scaler(b) + scaler(b + 1) * scaler(b) * twoinv * scaler(k - 1)) *
             scaler(curr);
      ans += scaler(a - b) * scaler(rw - i) * scaler(curr);
      ans += (scaler(rw - lw) * scaler(c - a) -
              (scaler(c) * scaler(c + 1) - scaler(a) * scaler(a + 1)) * twoinv *
                  scaler(k - 1)) *
             scaler(curr);
    }
  }
  std::cout << ans.v << std::endl;
  return 0;
}

### Prompt

Please provide a cpp coded solution 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 ul = std::uint32_t;
using ull = std::uint64_t;
using li = std::int32_t;
using ll = std::int64_t;
using vul = std::vector<ul>;
const ul base = 1e9 + 7;
const ul sz = 1 << 20;
void insert(ul pos, ul v, vul& tree) {
  for (tree[pos += sz] = v, pos >>= 1; pos; pos >>= 1) {
    tree[pos] = std::max(tree[pos << 1], tree[pos << 1 | 1]);
  }
}
ul query(ul l, ul r, const vul& tree) {
  ul ret = 0;
  for (l = l + sz - 1, r = r + sz; l ^ r ^ 1; l >>= 1, r >>= 1) {
    if (~l & 1) {
      ret = std::max(ret, tree[l ^ 1]);
    }
    if (r & 1) {
      ret = std::max(ret, tree[r ^ 1]);
    }
  }
  return ret;
}
ul plus(ul a, ul b) { return a + b < base ? a + b : a + b - base; }
ul minus(ul a, ul b) { return a < b ? a + base - b : a - b; }
ul times(ul a, ul b) { return ull(a) * b % base; }
class scaler {
 public:
  ul v = 0;
  scaler(ul x) : v(x) {}
  scaler() = default;
};
const scaler twoinv(5e8 + 4);
scaler operator+(const scaler& a, const scaler& b) { return plus(a.v, b.v); }
scaler& operator+=(scaler& a, const scaler& b) {
  a.v = plus(a.v, b.v);
  return a;
}
scaler operator-(const scaler& a, const scaler& b) { return minus(a.v, b.v); }
scaler& operator-=(scaler& a, const scaler& b) {
  a.v = minus(a.v, b.v);
  return a;
}
scaler operator*(const scaler& a, const scaler& b) { return times(a.v, b.v); }
scaler& operator*=(scaler& a, const scaler& b) {
  a.v = times(a.v, b.v);
  return a;
}
li upper(li a, li b) {
  li temp = a / b;
  return temp * b < a ? temp + li(1) : temp;
}
vul tree1(1 << 21, 0), tree2(1 << 21, 0);
ul n, k;
vul datas(1e6 + 1);
vul lws(1e6 + 1);
vul rws(1e6 + 1);
vul datascopy(1e6 + 1);
vul datascopy2(1e6 + 1);
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  std::cin >> n >> k;
  for (ul i = 1; i <= n; ++i) {
    std::cin >> datas[i];
  }
  datascopy = datas;
  datascopy2 = datas;
  std::sort(datascopy.begin() + 1, datascopy.begin() + n + 1);
  for (ul i = 1; i <= n; ++i) {
    datas[i] = std::lower_bound(datascopy.begin() + 1,
                                datascopy.begin() + n + 1, datas[i]) -
               datascopy.begin();
  }
  for (ul i = 1; i <= n; ++i) {
    lws[i] = query(datas[i], sz, tree1) + 1;
    insert(datas[i], i, tree1);
  }
  for (ul i = n; i; --i) {
    rws[i] = n + 1 - query(datas[i] + 1, sz, tree2);
    insert(datas[i], n + 1 - i, tree2);
  }
  scaler ans;
  for (ul i = 1; i <= n; ++i) {
    ul curr = datascopy2[i];
    ul lw = lws[i];
    ul rw = rws[i];
    li a = std::max(upper(li(i - lw), k - 1) - li(1), li(0));
    li b = std::max(upper(li(rw - i) - 1, k - 1) - li(1), li(0));
    li c = (li(rw - lw) - 1) / li(k - 1);
    if (a <= b) {
      ans += (scaler(a) + scaler(a + 1) * scaler(a) * twoinv * scaler(k - 1)) *
             scaler(curr);
      ans += scaler(b - a) * scaler(i + 1 - lw) * scaler(curr);
      ans += (scaler(rw - lw) * scaler(c - b) -
              (scaler(c) * scaler(c + 1) - scaler(b) * scaler(b + 1)) * twoinv *
                  scaler(k - 1)) *
             scaler(curr);
    }
    if (b < a) {
      ans += (scaler(b) + scaler(b + 1) * scaler(b) * twoinv * scaler(k - 1)) *
             scaler(curr);
      ans += scaler(a - b) * scaler(rw - i) * scaler(curr);
      ans += (scaler(rw - lw) * scaler(c - a) -
              (scaler(c) * scaler(c + 1) - scaler(a) * scaler(a + 1)) * twoinv *
                  scaler(k - 1)) *
             scaler(curr);
    }
  }
  std::cout << ans.v << std::endl;
  return 0;
}

```