#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[1000309];
int nxt[1000309], prv[1000309], dp[1000309];
int main() {
  while (scanf("%d %d", &n, &k) != EOF) {
    dp[0] = 0;
    for (int i = 1; (i) <= int(n); (i)++) {
      dp[i] = (dp[i - 1] + (i - 1) / (k - 1)) % 1000000007;
    }
    for (int i = 0; (i) < int(n); (i)++) scanf("%d", &a[i]);
    stack<int> sp, sn;
    for (int i = 0; (i) < int(n); (i)++) {
      while (!sp.empty() && a[sp.top()] < a[i]) sp.pop();
      prv[i] = sp.empty() ? -1 : sp.top();
      sp.push(i);
    }
    for (int i = int(n) - 1; (i) >= 0; (i)--) {
      while (!sn.empty() && a[sn.top()] <= a[i]) sn.pop();
      nxt[i] = sn.empty() ? n : sn.top();
      sn.push(i);
    }
    int ans = 0;
    for (int i = 0; (i) < int(n); (i)++) {
      int r = nxt[i];
      int l = prv[i];
      int cur = ((dp[r - l - 1] - dp[i - l - 1] - dp[r - i - 1]) % 1000000007 +
                 1000000007) %
                1000000007;
      cur = (a[i] * 1ll * cur) % 1000000007;
      ans = (ans + cur) % 1000000007;
    }
    printf("%d\n", ans);
  }
  return 0;
}
