#include <bits/stdc++.h>
const int maxn = 1E6 + 77;
const int mod = 1E9 + 7;
int N, K, A[maxn], L[maxn], R[maxn], X[maxn];
bool cmp(int a, int b) { return A[a] < A[b]; }
int ans;
void update_ans(int a, int b, int p) {
  int ret = 0;
  long long t;
  if (a > b) std::swap(a, b);
  int ia = (a - 1) / (K - 1), ib = (b - 1) / (K - 1),
      ic = (a + b - 1) / (K - 1);
  ret += ia;
  if (ret >= mod) ret -= mod;
  t = (ia * (ia + 1ll) / 2) % mod;
  t = t * (K - 1) % mod;
  ret += t;
  if (ret >= mod) ret -= mod;
  ret += a * (long long)(ib - ia) % mod;
  if (ret >= mod) ret -= mod;
  ret += (a + b) * (long long)(ic - ib) % mod;
  if (ret >= mod) ret -= mod;
  ret -= (((ib + ic + 1ll) * (K - 1) + 2) * (ic - ib) / 2) % mod;
  if (ret < 0) ret += mod;
  ans += ret * (long long)A[p] % mod;
  if (ans >= mod) ans -= mod;
}
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; ++i) {
    scanf("%d", A + i);
    X[i] = i;
  }
  std::sort(X + 1, X + N + 1, cmp);
  for (int i = 1; i <= N; ++i) {
    int p = X[i], l = p, r = p;
    if (L[p - 1]) l = L[p - 1];
    if (R[p + 1]) r = R[p + 1];
    L[l] = L[r] = l;
    R[l] = R[r] = r;
    update_ans(p - l + 1, r - p + 1, p);
  }
  printf("%d\n", ans);
}
