#include <bits/stdc++.h>
template <typename Y>
inline bool updmin(Y &a, Y b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename Y>
inline bool updmax(Y &a, Y b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename Y>
inline Y abs(Y a) {
  if (a < 0) a = -a;
  return a;
}
template <typename Y>
inline Y sqr(Y a) {
  return a * a;
}
int read() {
  int w = 1, q = 0, ch = ' ';
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') w = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) q = q * 10 + ch - 48;
  return q * w;
}
inline void FileIO() {
  freopen(
      ""
      ".in",
      "r", stdin);
  freopen(
      ""
      ".out",
      "w", stdout);
}
const int N = 1000010;
int n;
long long k, a[N], l[N], r[N];
int st[N], tp = 0;
int main() {
  n = read(), k = read() - 1;
  for (int i = 1; i <= n; i++) {
    a[i] = read();
  }
  a[0] = a[n + 1] = 0x3f3f3f3f;
  st[tp++] = 0;
  for (int i = 1; i <= n; i++) {
    while (a[st[tp - 1]] <= a[i]) {
      tp--;
    }
    l[i] = st[tp - 1];
    st[tp++] = i;
  }
  tp = 0;
  st[tp++] = n + 1;
  for (int i = n; i; i--) {
    while (a[st[tp - 1]] < a[i]) {
      tp--;
    }
    r[i] = st[tp - 1];
    st[tp++] = i;
  }
  long long ans = 0, mod = 1000000007;
  for (int i = 1; i <= n; i++) {
    long long res = 0;
    long long x = i - l[i], y = r[i] - i;
    if (x > y) std::swap(x, y);
    long long n1 = (x - 1) / k;
    (res += n1 + n1 * (n1 + 1) / 2 * k) %= mod;
    long long n2 = (y - 1) / k - n1;
    (res += n2 * x) %= mod;
    long long u = x - ((n1 + n2 + 1) * k + 1 - y);
    if (u > 0) {
      long long n3 = u / k + 1, d = u - (n3 - 1) * k;
      (res += (u + d) * n3 / 2) %= mod;
    }
    (ans += res * a[i]) %= mod;
  }
  printf("%d\n", (int)ans);
  return 0;
}
