#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, mod = 1e9 + 7;
int n, k, h, ans, a[N], st[N], l[N], r[N];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
  return f ? x : -x;
}
int calc(int x) {
  int y = (x - 1) / (k - 1);
  return (1ll * x * y - (1ll * (y + 1) * y >> 1) % mod * (k - 1)) % mod;
}
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    while (h && a[i] >= a[st[h]]) h--;
    l[i] = st[h], st[++h] = i;
  }
  st[h = 0] = n + 1;
  for (int i = n; i; i--) {
    while (h && a[i] > a[st[h]]) h--;
    r[i] = st[h], st[++h] = i;
  }
  for (int i = 1; i <= n; i++)
    ans = (ans + 1ll *
                     (calc(r[i] - l[i] - 1) - calc(i - l[i] - 1) -
                      calc(r[i] - i - 1)) *
                     a[i]) %
          mod;
  printf("%d", (ans + mod) % mod);
  return 0;
}
