#include <bits/stdc++.h>
using namespace std;
const int MX = 1e6 + 5;
const int mod = 1e9 + 7;
int n, k;
int a[MX], l[MX], r[MX];
stack<pair<int, int> > s;
long long cal(int l, int r) {
  if (l > r) return 0;
  if (r - l + 1 < k) return 0;
  int len = (r - l + 2 - k) / (k - 1);
  long long res = 1ll * (len + 1) * (r - l + 2 - k) % mod -
                  1ll * (1 + len) * len / 2 % mod * (k - 1) % mod;
  return res;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    if (i == 1)
      s.push(make_pair(a[i], i)), l[i] = i;
    else {
      l[i] = i;
      while (!s.empty() && s.top().first < a[i]) l[i] = s.top().second, s.pop();
      s.push(make_pair(a[i], l[i]));
    }
  }
  while (!s.empty()) s.pop();
  for (int i = n; i >= 1; i--) {
    if (i == n)
      s.push(make_pair(a[i], i)), r[i] = i;
    else {
      r[i] = i;
      while (!s.empty() && s.top().first <= a[i])
        r[i] = s.top().second, s.pop();
      s.push(make_pair(a[i], r[i]));
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long res = cal(l[i], r[i]) - cal(l[i], i - 1) - cal(i + 1, r[i]);
    res = res % mod * a[i] % mod;
    ans = (ans + res) % mod;
  }
  if (ans < 0) ans += mod;
  cout << ans << endl;
  return 0;
}
