#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool upmin(T &x, T y) {
  return y <= x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
  return x <= y ? x = y, 1 : 0;
}
const long double eps = 1e-9;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = (1ll << 62) - 1;
const int MAXN = 1000005;
const int mods = 1e9 + 7;
const int MX = 100000;
const int inv2 = (mods + 1) >> 1;
const int INF = 0x3f3f3f3f;
namespace FastIO {
constexpr int SIZE = (1 << 21) + 1;
int num = 0, f;
char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf,
                                                *oT = obuf + SIZE - 1, c;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char c) {
  *oS++ = c;
  if (oS == oT) flush();
}
inline void getc(char &c) {
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       !isalpha(c) && c != EOF;
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
}
inline void reads(char *st) {
  char c;
  int n = 0;
  getc(st[++n]);
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       isalpha(c);
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    st[++n] = c;
  st[++n] = '\0';
}
template <class I>
inline void read(I &x) {
  for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 3) + (x << 1) + (c & 15);
  x *= f;
}
template <class I>
inline void print(I x) {
  if (x < 0) putc('-'), x = -x;
  if (!x) putc('0');
  while (x) que[++num] = x % 10 + 48, x /= 10;
  while (num) putc(que[num--]);
}
inline void putstr(string st) {
  for (int i = 0; i < (int)st.size(); ++i) putc(st[i]);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_Flusher_;
}  // namespace FastIO
using FastIO ::print;
using FastIO ::putc;
using FastIO ::putstr;
using FastIO ::read;
using FastIO ::reads;
int Log[MAXN], a[MAXN], mx[20][MAXN], id[20][MAXN], ans, n, K;
int upd(int x, int y) { return x + y >= mods ? x + y - mods : x + y; }
int getmx(int l, int r) {
  int t = Log[r - l + 1];
  if (mx[t][l] >= mx[t][r - (1 << t) + 1]) return id[t][l];
  return id[t][r - (1 << t) + 1];
}
int getsum(int x, int y) {
  return upd(1ll * x * y % mods,
             mods - 1ll * y * (y - 1) / 2 % mods * (K - 1) % mods);
}
int getans(int n, int id) {
  int L = (n - 1) / (K - 1), x, y, ans = 0;
  x = (n - id) / (K - 1);
  ans = upd(ans, 1ll * x * id % mods);
  y = L - x;
  ans = upd(ans, getsum(n - (x + 1) * (K - 1), y));
  x = (id - 1) / (K - 1);
  ans = upd(ans, mods - getsum(id - K, x));
  return ans;
}
void solve(int l, int r) {
  if (r - l + 1 < K) return;
  int t = getmx(l, r);
  solve(l, t - 1), solve(t + 1, r);
  ans = upd(ans, 1ll * a[t] * getans(r - l + 1, t - l + 1) % mods);
}
signed main() {
  read(n), read(K);
  for (int i = 1; i <= n; ++i) read(a[i]), mx[0][i] = a[i], id[0][i] = i;
  Log[1] = 0;
  for (int i = 2; i <= n; ++i) Log[i] = Log[i >> 1] + 1;
  for (int i = 1; i <= Log[n]; ++i)
    for (int j = 1; j <= n - (1 << i) + 1; ++j)
      mx[i][j] = max(mx[i - 1][j], mx[i - 1][j + (1 << (i - 1))]),
      id[i][j] = (mx[i][j] == mx[i - 1][j] ? id[i - 1][j]
                                           : id[i - 1][j + (1 << (i - 1))]);
  solve(1, n);
  print(ans);
  return 0;
}
