#include <bits/stdc++.h>
using namespace std;
const int N = 1000010, INF = 1e9 + 7, M = 1e9 + 7;
int n, k, a[N], lpos[N], rpos[N], ans;
int HALF(int x) { return x & 1 ? (x + M) >> 1 : x >> 1; }
int dcsl(int ss, int xs, int gc) {
  if (xs <= 0) return 0;
  int mx = ss + (xs - 1) * gc;
  return HALF((long long)(ss + mx) % M * xs % M);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  vector<int> g;
  a[0] = a[n + 1] = INF;
  g.push_back(0);
  for (int i = 1; i <= n; ++i) {
    int ret = 0;
    for (int l = 0, r = g.size() - 1, mid = (l + r) >> 1; l <= r;
         mid = (l + r) >> 1)
      if (a[g[mid]] > a[i])
        ret = mid, l = mid + 1;
      else
        r = mid - 1;
    lpos[i] = g[ret];
    while (a[i] >= a[g.back()]) g.pop_back();
    g.push_back(i);
  }
  g.clear();
  g.push_back(n + 1);
  for (int i = n; i >= 1; --i) {
    int ret = 0;
    for (int l = 0, r = g.size() - 1, mid = (l + r) >> 1; l <= r;
         mid = (l + r) >> 1)
      if (a[g[mid]] >= a[i])
        ret = mid, l = mid + 1;
      else
        r = mid - 1;
    rpos[i] = g[ret];
    while (a[i] >= a[g.back()]) g.pop_back();
    g.push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    int t1 = (i - lpos[i] - 1) / (k - 1), t2 = (rpos[i] - i - 1) / (k - 1),
        ret = 0;
    if (t1 < t2) {
      (ret += dcsl(k, t1, k - 1)) %= M;
      (ret += (long long)(i - lpos[i]) * (t2 - t1) % M) %= M;
      (ret += dcsl(rpos[i] - (k - 1) * (t2 + 1) - lpos[i] - 1,
                   (rpos[i] - lpos[i] - 1) / (k - 1) - t2, 1 - k)) %= M;
    } else {
      (ret += dcsl(k, t2, k - 1)) %= M;
      (ret += (long long)(rpos[i] - i) * (t1 - t2) % M) %= M;
      (ret += dcsl(rpos[i] - (k - 1) * (t1 + 1) - lpos[i] - 1,
                   (rpos[i] - lpos[i] - 1) / (k - 1) - t1, 1 - k)) %= M;
    }
    (ans += (long long)ret * a[i] % M) %= M;
  }
  cout << ans;
}
