output_description: Print the number of permutations that can be produced as P after the
  operation.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N K
      P_0 P_1 \cdots P_{N-1}
attempt: import unittest
  import collections
  
  
  def solve(n, k, ps):
      if n == k:
          return 1
  
      valid_point = [False] * (n + k + 1)
      valid = [0] * (n + k + 1)
      for i in range(n - 1):
          if ps[i] > ps[i + 1]:
              valid_point[i + 1] = True
              valid[i] -= 1
              valid[i + k - 1] += 1
      for i in range(n + k - 1, -1, -1):
          valid[i] += valid[i + 1]
  
      min_dq = collections.deque()
      max_dq = collections.deque()
      min_dq.append(0)
      max_dq.append(0)
      for i in range(1, k - 1):
          if min_dq[0] < i - k + 1:
              min_dq.popleft()
          while min_dq and ps[min_dq[-1]] >= ps[i]:
              min_dq.pop()
          min_dq.append(i)
          if max_dq[0] < i - k + 1:
              max_dq.popleft()
          while max_dq and ps[max_dq[-1]] <= ps[i]:
              max_dq.pop()
          max_dq.append(i)
      pre_min = min_dq[0]
      pre_max = max_dq[0]
  
      cnt = 0
      has_zero = False
      for i in range(k - 1, n):
          pre_min = ps[min_dq[0]]
          pre_max = ps[max_dq[0]]
          if min_dq[0] < i - k + 1:
              min_dq.popleft()
          while min_dq and ps[min_dq[-1]] >= ps[i]:
              min_dq.pop()
          min_dq.append(i)
          if max_dq[0] < i - k + 1:
              max_dq.popleft()
          while max_dq and ps[max_dq[-1]] <= ps[i]:
              max_dq.pop()
          max_dq.append(i)
  
          if valid[i] != 0:
              if cnt == 0:
                  cnt += 1
                  continue
              if (
                  cnt == 0
                  or valid[i - 1] != valid[i]
                  or (pre_max != ps[i - 1] and ps[min_dq[0]] != ps[i - k + 1])
                  or (pre_min != ps[i - k] and ps[max_dq[0]] != ps[i])
              ):
                  if pre_min == ps[i - k] and max_dq[0] == i:
                      continue
                  cnt += 1
          else:
              has_zero = True
      if has_zero:
          cnt += 1
      return cnt
  
  
  def main():
      n, k = map(int, input().split())
      ps = list(map(int, input().split()))
      print(solve(n, k, ps))
  
  
  class TestAcd038B(unittest.TestCase):
      def test_agc038b(self):
          self.assertEqual(solve(5, 3, list(map(int, "0 2 1 4 3".split()))), 2)
          self.assertEqual(solve(4, 4, list(map(int, "0 1 2 3".split()))), 1)
          self.assertEqual(solve(10, 4, list(map(int, "2 0 1 3 7 5 4 6 8 9".split()))), 6)
          self.assertEqual(solve(10, 2, list(map(int, "1 2 3 4 5 6 7 8 9 0".split()))), 2)
          self.assertEqual(solve(10, 4, list(map(int, "0 1 2 4 3 5 6 8 7 9".split()))), 3)
  
  
  if __name__ == "__main__":
      # main()
      unittest.main()
  
problem_description: Statement
  
  Snuke has a permutation (P_0,P_1,\cdots,P_{N-1}) of (0,1,\cdots,N-1).
  
  Now, he will perform the following operation **exactly once** :
  
    * Choose K consecutive elements in P and sort them in ascending order.
  
  Find the number of permutations that can be produced as P after the operation.
samples: [{"input": "5 3\n    0 2 1 4 3", "output": "2\n    \n\nTwo permutations can be produced as P after the operation: (0,1,2,4,3) and\n(0,2,1,3,4).\n\n* * *"}, {"input": "4 4\n    0 1 2 3", "output": "1\n    \n\n* * *"}, {"input": "10 4\n    2 0 1 3 7 5 4 6 8 9", "output": "6"}]