#include <bits/stdc++.h>
using namespace std;
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(long long &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const long long &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.first);
  putchar(' ');
  _W(x.second);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
int MOD = 1e9 + 7;
void ADD(long long &x, long long v) {
  x = (x + v) % MOD;
  if (x < 0) x += MOD;
}
const int SIZE = 1e6 + 10;
int nxt[SIZE], pre[SIZE];
int K;
long long f(int n, int x) {
  if (n < K) return 0;
  long long ret = 0;
  long long all_lv = (n - 1) / (K - 1) + 1;
  long long normal_lv = (n - x) / (K - 1) + 1;
  ADD(ret, (2 + (normal_lv - 1) * (K - 1)) * normal_lv / 2);
  ADD(ret, (n - x + 1) * (all_lv - normal_lv));
  int decrease_lv = (x - 1) / (K - 1) + 2;
  int low = (decrease_lv - 1) * (K - 1) - (x - 1);
  ADD(ret, -(low + low + (all_lv - decrease_lv) * (K - 1)) *
               (all_lv - decrease_lv + 1) / 2);
  ADD(ret, -1);
  return ret;
}
int main() {
  int n;
  R(n, K);
  vector<pair<int, int> > AA;
  for (int i = (1); i <= (n); ++i) {
    int x;
    R(x);
    nxt[i] = i + 1;
    pre[i] = i - 1;
    AA.push_back({x, i});
  }
  sort((AA).begin(), (AA).end());
  long long an = 0;
  for (int i = 0; i < (n); ++i) {
    int v = AA[i].first;
    int x = AA[i].second;
    int ll = pre[x];
    int rr = nxt[x];
    ADD(an, v * f(rr - ll - 1, x - ll));
    nxt[ll] = rr;
    pre[rr] = ll;
  }
  W(an);
  return 0;
}
