#include <bits/stdc++.h>   using namespace std;   template <class T1, class T2>   inline void upd1(T1& a, const T2& b) {     a = a < b ? a : b;   }   template <class T1, class T2>   inline void upd2(T1& a, const T2& b) {     a = a > b ? a : b;   }   template <class T>   inline bool equ(const T& a, const T& b) {     return !memcmp(&a, &b, sizeof a);   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   struct ano {     operator long long() {       long long x = 0, y = 0, c = getchar();       while (c < 48) y = c == 45, c = getchar();       while (c > 47) x = x * 10 + c - 48, c = getchar();       return y ? -x : x;     }   } buf;   const int p = 1e9 + 7;   const int N = 1e6 + 5;   int w[N], a[N], l[N], r[N];   long long cal(long long a, long long n, long long d) {     return (a * n + n * (n - 1) / 2 % p * d) % p;   }   int main() {     int n = buf, m = buf;     for (int i = 1; i <= n; ++i) w[i] = buf;     w[n + 1] = 1 << 30;     int j = 0;     for (int i = 1; i <= n + 1; ++i) {       while (j > 0 && w[a[j]] <= w[i]) r[a[j--]] = i - 1;       l[i] = a[j] + 1;       a[++j] = i;     }     long long ans = 0;     for (int i = 1; i <= n; ++i) {       int a = i - l[i] + 1;       int b = r[i] - i + 1;       int c = (min(a, b) - 1) / (m - 1);       long long s = cal(m, c, m - 1);       int v = (c + 1) * (m - 1) + 1;       if (max(a, b) >= v) {         long long d = (max(a, b) - v) / (m - 1);         s += ((d + 1) * min(a, b)) % p;         v += (d + 1) * (m - 1);       }       v = a + b - v;       if (v >= 1) s += cal(v, (v - 1) / (m - 1) + 1, 1 + p - m);       ans += w[i] * s % p;     }     cout << ans % p << endl;   }   