/**
 * BaZ :D
 */
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main
{
    static Reader scan;
    static PrintWriter pw;
    static int n,k,left[],right[],arr[];
    static long MOD = 1000000007,count[],dp[];
    public static void main(String[] args) {
        new Thread(null,null,"BaZ",1<<25)
        {
            public void run()
            {
                try
                {
                    solve();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }.start();
    }
    static void solve() throws IOException
    {
        scan = new Reader();
        pw = new PrintWriter(System.out,true);
        StringBuilder sb = new StringBuilder();
        n = ni();
        k = ni();
        int stack[] = new int[1000001];
        int top = -1;
        arr = new int[n];
        left = new int[n];
        right = new int[n];
        for(int i=0;i<n;++i) {
            arr[i] = ni();
            while(top>=0 && arr[stack[top]]<=arr[i])
                top--;
            if(top==-1)
                left[i] = 0;
            else left[i] = stack[top]+1;
            stack[++top] = i;
        }
        top = -1;
        for(int i=n-1;i>=0;--i) {
            while(top>=0 && arr[stack[top]]<arr[i])
                top--;
            if(top==-1)
                right[i] = n-1;
            else right[i] = stack[top]-1;
            stack[++top] = i;
        }
        //pa("left", left);
        //pa("right", right);
        dp = new long[n+1];
        for(int i=0;i<=n;++i) {
            if(i<k)
              continue;
            dp[i] = dp[i-k+1] + (i-k+1);
        }
        count = new long[n];
        long ans = 0;
        for(int i=0;i<n;++i) {
            int len = right[i]-left[i]+1;
            int lef = i-left[i];
            int rig = right[i]-i;
            long count = dp[len] - dp[lef] - dp[rig];
            if(count>=MOD)
             ... What is the time complexity of the previous code?