#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long maxn = 1e6 + 222;
long long ori[maxn];
long long L[maxn], R[maxn];
long long k;
inline long long cal1(long long l, long long r) {
  return (1ll * (l / (k - 1) + 1) * (r / (k - 1) + 1) - 1) % mod;
}
inline long long cal(long long l, long long r) {
  long long ans = 0;
  long long p = k - 1;
  long long tot = min(r + 1, k - 2);
  while (tot) {
    long long lmod = l % p;
    long long rmod = r % p;
    long long t = min(p - lmod, rmod + 1);
    t = min(t, tot);
    tot -= t;
    ans += 1ll * t * (l / p) * (r / p + 1) % mod;
    l += t;
    r -= t;
  }
  return ans;
}
stack<pair<long long, long long> > st;
vector<long long> v;
map<long long, vector<long long> > vv;
long long ma[maxn];
void solve() {
  long long n;
  scanf("%lld %lld", &n, &k);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &ori[i]);
    v.push_back(ori[i]);
  }
  sort(v.begin(), v.end());
  for (long long i = 0; i < n; i++) {
    vv[v[i]].push_back(i);
  }
  for (long long i = 1; i <= n; i++) {
    ma[i] = vv[ori[i]].back();
    vv[ori[i]].pop_back();
  }
  for (long long i = 1; i <= n; i++) {
    R[i] = n;
    while (!st.empty() && st.top().first < ma[i]) {
      R[st.top().second] = i - 1;
      st.pop();
    }
    if (st.empty())
      L[i] = 1;
    else
      L[i] = st.top().second + 1;
    st.push(make_pair(ma[i], i));
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long cnt = 0;
    long long llen = i - L[i];
    long long rlen = R[i] - i;
    if (R[i] - i - 1 >= 0) cnt += cal(llen + 1, rlen - 1);
    cnt += cal1(llen, rlen);
    ans += 1ll * cnt * v[ma[i]] % mod;
    ans %= mod;
  }
  cout << ans << endl;
}
signed main() { solve(); }
