#include <bits/stdc++.h>
using namespace std;
using namespace rel_ops;
using ll = int64_t;
using Pii = pair<int, int>;
using ull = uint64_t;
using Vi = vector<int>;
void run();
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  run();
  return 0;
}
int uplg(int n) { return 32 - __builtin_clz(n); }
int uplg(ll n) { return 64 - __builtin_clzll(n); }
constexpr ll MOD = 1e9 + 7;
struct Zp {
  ll first;
  Zp(ll a = 0) {
    if (a < 0)
      a = a % MOD + MOD;
    else if (a >= MOD * 2)
      a %= MOD;
    else if (a >= MOD)
      a -= MOD;
    first = a;
  }
  Zp operator+(Zp r) const { return first + r.first; }
  Zp operator-(Zp r) const { return first - r.first + MOD; }
  Zp operator*(Zp r) const { return first * r.first; }
  Zp operator/(Zp r) const { return first * r.inv().first; }
  Zp operator-() const { return MOD - first; }
  Zp inv() const { return pow(MOD - 2); }
  Zp pow(ll e) const {
    Zp t = 1, m = *this;
    while (e) {
      if (e & 1) t *= m;
      e >>= 1;
      m *= m;
    }
    return t;
  }
  Zp& operator+=(Zp r) { return *this = *this + r; }
  Zp& operator-=(Zp r) { return *this = *this - r; }
  Zp& operator*=(Zp r) { return *this = *this * r; }
  Zp& operator/=(Zp r) { return *this = *this / r; }
  void print() { cerr << first; }
};
vector<Zp> pointCovers, counts;
void run() {
  int n, k;
  cin >> n >> k;
  Vi seq(n);
  for (auto& first : (seq)) cin >> first;
  Vi prev(n), next(n);
  prev[0] = -1;
  next.back() = n;
  for (int i = (1); i < (n); i++) {
    int j = i - 1;
    while (j >= 0 && seq[j] <= seq[i]) j = prev[j];
    prev[i] = j;
  }
  for (int i = n - 2; i >= 0; i--) {
    int j = i + 1;
    while (j < n && seq[j] < seq[i]) j = next[j];
    next[i] = j;
  }
  pointCovers.resize(n + 5);
  for (int i = (k); i < (int((pointCovers).size())); i++) {
    pointCovers[i] = pointCovers[i - 1];
    if ((i - k) % (k - 1) == 0) {
      pointCovers[i] += i;
    }
  }
  counts.resize(n + 5);
  for (int i = (k); i < (int((counts).size())); i++) {
    counts[i] = counts[i - 1];
    if ((i - k) % (k - 1) == 0) {
      counts[i] += 1;
    }
  }
  Zp ans = 0;
  for (int i = (0); i < (n); i++) {
    ll elem = seq[i];
    int pos = i - prev[i] - 1;
    int len = next[i] - prev[i] - 1;
    pos = min(pos, len - pos - 1);
    if (len < k) continue;
    int a = pos + 1;
    int b = len - pos;
    Zp inters = 0;
    if (a > 0) inters += pointCovers[a];
    if (b > a) inters += (counts[b] - counts[a]) * (pos + 1);
    if (b < len) {
      int minX = (b - 1) / (k - 1);
      int maxX = (len - k) / (k - 1);
      if (minX <= maxX) {
        inters += ll(len - k + 1) * (maxX - minX + 1);
        Zp prog = ll(maxX) * (maxX + 1) / 2 - ll(minX - 1) * (minX) / 2;
        inters -= prog * (k - 1);
      }
    }
    ans += inters * elem;
  }
  cout << ans.first << endl;
}
