#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const long long mod = 1e9 + 7;
long long f[N];
struct pos {
  int x;
  int p;
} a[N];
bool cmp(pos a, pos b) { return a.x > b.x; }
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (long long i = k; i <= n; i++) f[i] = (f[i + 1 - k] + i - k + 1) % mod;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[i].p = i;
  }
  set<int> s;
  s.insert(0), s.insert(n + 1);
  long long ans = 0;
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    auto it = s.lower_bound(a[i].p);
    int r = *it, l = *(--it);
    s.insert(a[i].p);
    long long now =
        ((f[r - l - 1] - f[r - a[i].p - 1] - f[a[i].p - l - 1]) % mod + mod) %
        mod;
    ans = (ans + (now * a[i].x) % mod) % mod;
  }
  cout << ans % mod << endl;
  return 0;
}
