#include <bits/stdc++.h>
using namespace std;
const int mo = 1000000007;
const int N = 1000005;
int k, n, a[N], l[N], r[N], q[N], ans;
int F(int l) {
  int pr = 5000000 / k * k;
  int pl = l + (k - l % k) % k;
  return 1ll * (pr + pl - 2 * l + 2) * (pr / k - pl / k + 1) / 2 % mo;
}
int Func(int vl, int vr) {
  int ans = F(0);
  ans = (ans + mo - F(vl)) % mo;
  ans = (ans + mo - F(vr)) % mo;
  ans = (ans + F(vl + vr)) % mo;
  return ans + mo - 1;
}
int main() {
  scanf("%d%d", &n, &k);
  --k;
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &a[i]);
  for (int i = (int)(1); i <= (int)(n); i++) l[i] = 0, r[i] = n + 1;
  int t = 1;
  q[1] = 1;
  for (int i = (int)(2); i <= (int)(n); i++) {
    for (; t && a[q[t]] <= a[i]; --t) r[q[t]] = i;
    l[i] = q[t];
    q[++t] = i;
  }
  for (int i = (int)(1); i <= (int)(n); i++) {
    ans = (ans + 1ll * a[i] * Func(i - l[i], r[i] - i)) % mo;
  }
  cout << ans << endl;
}
