func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6 + 5;   const int INF = 0x3f3f3f3f;   const long long mod = 1e9 + 7;   int n, k;   int a[MAXN];   stack<pair<int, int> > s;   int l[MAXN], r[MAXN];   long long cal(int l, int r) {     if (l > r || r - l + 1 - k < 0) return 0;     int m = (r - l + 1 - (k - 1)) / (k - 1);     long long res = 1LL * (m + 1) * (r - l - k + 2) % mod -                     1LL * (1 + m) * m / 2 % mod * (k - 1) % mod;     return res;   }   int main(int argc, char* argv[]) {     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , a + i);     for (int i = 1; i <= n; i++) {       if (i == 1)         s.push(make_pair(a[i], l[i] = 1));       else {         l[i] = i;         while (!s.empty() && s.top().first < a[i]) {           l[i] = s.top().second;           s.pop();         }         s.push(make_pair(a[i], l[i]));       }     }     while (!s.empty()) s.pop();     for (int i = n; i >= 1; i--) {       if (i == n)         s.push(make_pair(a[i], r[i] = n));       else {         r[i] = i;         while (!s.empty() && s.top().first <= a[i]) {           r[i] = s.top().second;           s.pop();         }         s.push(make_pair(a[i], r[i]));       }     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       long long res = cal(l[i], r[i]) - cal(l[i], i - 1) - cal(i + 1, r[i]);       res = res % mod * a[i] % mod;       ans = (ans + res) % mod;     }     if (ans < 0) ans += mod;     printf( %lld n , ans);     return 0;   }   