#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 7;
const int MOD = (int)1e9 + 7;
const long long INF = (long long)1e18 + 7;
long long a[N];
int L[N], R[N];
int n, k;
long long calc(int l, int r) {
  long long n = r - l + 1;
  if (n < k) return 0;
  long long x = (n - 1) / (k - 1);
  return (n * x - (k - 1LL) * x * (x + 1LL) / 2LL) % MOD;
  long long ans = x * (x + 1LL) / 2;
  ans = n * x - (k - 1) * ans;
  return ans % MOD;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  deque<int> q;
  a[0] = a[n + 1] = MOD;
  q.push_back(0);
  for (int i = 1; i <= n; i++) {
    while (a[q.back()] < a[i]) q.pop_back();
    L[i] = q.back() + 1;
    q.push_back(i);
  }
  q = {n + 1};
  for (int i = n; i >= 1; i--) {
    while (a[q.back()] <= a[i]) q.pop_back();
    R[i] = q.back() - 1;
    q.push_back(i);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long num = calc(L[i], R[i]) - calc(L[i], i - 1) - calc(i + 1, R[i]);
    ans += a[i] * num;
    ans %= MOD;
  }
  cout << ans;
}
