#include <bits/stdc++.h>   using namespace std;   const bool debug = false;   const int maxn = 1002003;   const long long magic = 1000 * 1000 * 1000 + 7;   int n, k;   long long aa[maxn];   struct RMQ {     int l, r;     RMQ *left, *right;     long long res;     int resIndex;   };   RMQ* build(long long* vs, int l, int r) {     RMQ* res = new RMQ();     res->l = l;     res->r = r;     if (r - l >= 2) {       int m = (l + r) / 2;       res->left = build(vs, l, m);       res->right = build(vs, m, r);       if (res->left->res >= res->right->res) {         res->res = res->left->res;         res->resIndex = res->left->resIndex;       } else {         res->res = res->right->res;         res->resIndex = res->right->resIndex;       }     } else {       res->left = nullptr;       res->right = nullptr;       res->res = vs[l];       res->resIndex = l;     }     return res;   }   pair<long long, int> rmqGet(RMQ* q, int l, int r) {     if (q->r <= l || r <= q->l) return make_pair(-100, -1);     if (l <= q->l && q->r <= r) return make_pair(q->res, q->resIndex);     auto [res1, res1index] = rmqGet(q->left, l, r);     auto [res2, res2index] = rmqGet(q->right, l, r);     if (res1 >= res2)       return make_pair(res1, res1index);     else       return make_pair(res2, res2index);   }   int ls[maxn];   int rs[maxn];   RMQ* rmq;   void findLR(int l, int r) {     if (debug) cout <<  findLR:   << l <<     << r << endl;     if (r - l < 2) return;     auto [res, resIndex] = rmqGet(rmq, l + 1, r);     if (debug)       cout <<  rmqGet:   << (l + 1) <<     << r <<     << res <<     << resIndex            << endl;     ls[resIndex] = l;     rs[resIndex] = r;     findLR(l, resIndex);     findLR(resIndex, r);   }   long long countLen(int len) {     if (len < k) return 0;     int rest = len - k;     long long count = rest / (k - 1);     long long res =         (len + 1) * (count + 1) - (count + 1) * (k + k + count * (k - 1)) / 2;     return res;   }   long long countIndex(int i) {     int len = rs[i] - (ls[i] + 1);     long long res =         countLen(len) - countLen(i - (ls[i] + 1)) - countLen(rs[i] - (i + 1));     if (debug) cout <<  countIndex:   << i <<     << res << endl;     return res;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> aa[i];     aa[0] = aa[n + 1] = 1200ll * 1000 * 1000;     rmq = build(aa, 0, n + 2);     findLR(0, n + 1);     if (debug)       for (int i = 1; i <= n; i++) cout << ls[i] <<     << rs[i] << endl;     long long res = 0;     for (int i = 1; i <= n; i++) {       long long c = countIndex(i) % magic;       res = (res + aa[i] * c) % magic;     }     cout << res << endl;     if (debug)       for (int i = 0; i <= 20; i++)         cout <<  len:   << i <<  :   << countLen(i) << endl;     return 0;   }   