#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50, mo = 1e9 + 7;
inline int gi() {
  char ch = getchar();
  int q = 0, x = 0;
  while (ch < '0' || ch > '9') q = (ch == '-' ? 1 : q), ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return q ? (-x) : x;
}
int a[N], l[N], r[N], st[N], tot, ans;
int main() {
  int n = gi(), k = gi() - 1;
  for (int i = 1; i <= n; ++i) a[i] = gi();
  for (int i = 1; i <= n; ++i) {
    while (tot && a[i] > a[st[tot]]) r[st[tot--]] = i - 1;
    st[++tot] = i;
  }
  while (tot) r[st[tot--]] = n;
  for (int i = n; i >= 1; --i) {
    while (tot && a[i] >= a[st[tot]]) l[st[tot--]] = i + 1;
    st[++tot] = i;
  }
  while (tot) l[st[tot--]] = 1;
  for (int i = 1; i <= n; ++i) {
    int x = i - l[i], y = r[i] - i, u = x / k, v = y / k, z = (x + y) / k;
    if (x > y) swap(x, y), swap(u, v);
    int sum = ((u + 1ll) * u / 2 * k + u + (v - u) * (x + 1ll) +
               (z - v) * (x + y + 1ll) -
               ((z + 1ll) * z / 2 - (v + 1ll) * v / 2) * k) %
              mo * a[i] % mo;
    ((ans += sum) %= mo);
  }
  cout << ans;
  return 0;
}
