Buggy python submission (verdict: Runtime Error):
from itertools import accumulate
class Segtree:
    def __init__(self, A, intv, initialize = True, segf = max):
        self.N = len(A)
        self.N0 = 2**(self.N-1).bit_length()
        self.intv = intv
        self.segf = segf
        if initialize:
            self.data = [intv]*self.N0 + A + [intv]*(self.N0 - self.N)
            for i in range(self.N0-1, 0, -1):
                self.data[i] = self.segf(self.data[2*i], self.data[2*i+1]) 
        else:
            self.data = [intv]*(2*self.N0)
        
    def update(self, k, x):
        k += self.N0
        self.data[k] = x
        while k > 0 :
            k = k >> 1
            self.data[k] = self.segf(self.data[2*k], self.data[2*k+1])
    
    def query(self, l, r):
        L, R = l+self.N0, r+self.N0
        s = self.intv
        while L < R:
            if R & 1:
                R -= 1
                s = self.segf(s, self.data[R])
            if L & 1:
                s = self.segf(s, self.data[L])
                L += 1
            L >>= 1
            R >>= 1
        return s
    
    def binsearch(self, l, r, check, reverse = False):
        L, R = l+self.N0, r+self.N0
        SL, SR = [], []
        while L < R:
            if R & 1:
                R -= 1
                SR.append(R)
            if L & 1:
                SL.append(L)
                L += 1
            L >>= 1
            R >>= 1
        
        if reverse:
            for idx in (SR + SL[::-1]):
                if check(self.data[idx]):
                    break
            else:
                return -1
            while idx < self.N0:
                if check(self.data[2*idx+1]):
                    idx = 2*idx + 1
                else:
                    idx = 2*idx
            return idx
        else:
            for idx in (SL + SR[::-1]):
                if check(self.data[idx]):
                    break
            else:
                return -1
            while idx < self.N0:
                if check(self.data[2*idx]):
                    idx = 2*idx
                else:
                    idx = 2*idx + 1
            return idx

readline = sys.stdin.readline
N, K = map(int, readline().split())
P = list(map(int, readline().split()))
STmax = Segtree(P, 0, True, max)
STmin = Segtree(P, 10**9, True, min)
ans = N - K + 1
upper = [0]*(N-1)
for i in range(N-1):
    if P[i] > P[i+1]:
        upper[i] = 1
upper = list(accumulate(upper)) + [0]
cnt = 0
for i in range(N-K):
    if upper[i+K-2] == upper[i-1]:
        cnt += 1
    elif P[i+K] == STmax.query(i+1, i+K+1) and P[i] == STmin.query(i, i+K):
        ans -= 1
if upper[N-2] == upper[N-K-1]:
    cnt += 1
print(ans - max(0, cnt-1))

Fixed submission:
import sys
from itertools import accumulate
class Segtree:
    def __init__(self, A, intv, initialize = True, segf = max):
        self.N = len(A)
        self.N0 = 2**(self.N-1).bit_length()
        self.intv = intv
        self.segf = segf
        if initialize:
            self.data = [intv]*self.N0 + A + [intv]*(self.N0 - self.N)
            for i in range(self.N0-1, 0, -1):
                self.data[i] = self.segf(self.data[2*i], self.data[2*i+1]) 
        else:
            self.data = [intv]*(2*self.N0)
        
    def update(self, k, x):
        k += self.N0
        self.data[k] = x
        while k > 0 :
            k = k >> 1
            self.data[k] = self.segf(self.data[2*k], self.data[2*k+1])
    
    def query(self, l, r):
        L, R = l+self.N0, r+self.N0
        s = self.intv
        while L < R:
            if R & 1:
                R -= 1
                s = self.segf(s, self.data[R])
            if L & 1:
                s = self.segf(s, self.data[L])
                L += 1
            L >>= 1
            R >>= 1
        return s
    
    def binsearch(self, l, r, check, reverse = False):
        L, R = l+self.N0, r+self.N0
        SL, SR = [], []
        while L < R:
            if R & 1:
                R -= 1
                SR.append(R)
            if L & 1:
                SL.append(L)
                L += 1
            L >>= 1
            R >>= 1
        
        if reverse:
            for idx in (SR + SL[::-1]):
                if check(self.data[idx]):
                    break
            else:
                return -1
            while idx < self.N0:
                if check(self.data[2*idx+1]):
                    idx = 2*idx + 1
                else:
                    idx = 2*idx
            return idx
        else:
            for idx in (SL + SR[::-1]):
                if check(self.data[idx]):
                    break
            else:
                return -1
            while idx < self.N0:
                if check(self.data[2*idx]):
                    idx = 2*idx
                else:
                    idx = 2*idx + 1
            return idx

readline = sys.stdin.readline
N, K = map(int, readline().split())
P = list(map(int, readline().split()))
STmax = Segtree(P, 0, True, max)
STmin = Segtree(P, 10**9, True, min)
ans = N - K + 1
upper = [0]*(N-1)
for i in range(N-1):
    if P[i] > P[i+1]:
        upper[i] = 1
upper = list(accumulate(upper)) + [0]
cnt = 0
for i in range(N-K):
    if upper[i+K-2] == upper[i-1]:
        cnt += 1
    elif P[i+K] == STmax.query(i+1, i+K+1) and P[i] == STmin.query(i, i+K):
        ans -= 1
if upper[N-2] == upper[N-K-1]:
    cnt += 1
print(ans - max(0, cnt-1))

Buggy python submission (verdict: Runtime Error):
from itertools import accumulate
class Segtree:
    def __init__(self, A, intv, initialize = True, segf = max):
        self.N = len(A)
        self.N0 = 2**(self.N-1).bit_length()
        self.intv = intv
        self.segf = segf
        if initialize:
            self.data = [intv]*self.N0 + A + [intv]*(self.N0 - self.N)
            for i in range(self.N0-1, 0, -1):
                self.data[i] = self.segf(self.data[2*i], self.data[2*i+1]) 
        else:
            self.data = [intv]*(2*self.N0)
        
    def update(self, k, x):
        k += self.N0
        self.data[k] = x
        while k > 0 :
            k = k >> 1
            self.data[k] = self.segf(self.data[2*k], self.data[2*k+1])
    
    def query(self, l, r):
        L, R = l+self.N0, r+self.N0
        s = self.intv
        while L < R:
            if R & 1:
                R -= 1
                s = self.segf(s, self.data[R])
            if L & 1:
                s = self.segf(s, self.data[L])
                L += 1
            L >>= 1
            R >>= 1
        return s
    
    def binsearch(self, l, r, check, reverse = False):
        L, R = l+self.N0, r+self.N0
        SL, SR = [], []
        while L < R:
            if R & 1:
                R -= 1
                SR.append(R)
            if L & 1:
                SL.append(L)
                L += 1
            L >>= 1
            R >>= 1
        
        if reverse:
            for idx in (SR + SL[::-1]):
                if check(self.data[idx]):
                    break
            else:
                return -1
            while idx < self.N0:
                if check(self.data[2*idx+1]):
                    idx = 2*idx + 1
                else:
                    idx = 2*idx
            return idx
        else:
            for idx in (SL + SR[::-1]):
                if check(self.data[idx]):
                    break
            else:
                return -1
            while idx < self.N0:
                if check(self.data[2*idx]):
                    idx = 2*idx
                else:
                    idx = 2*idx + 1
            return idx

readline = sys.stdin.readline
N, K = map(int, readline().split())
P = list(map(int, readline().split()))
STmax = Segtree(P, 0, True, max)
STmin = Segtree(P, 10**9, True, min)
ans = N - K + 1
upper = [0]*(N-1)
for i in range(N-1):
    if P[i] > P[i+1]:
        upper[i] = 1
upper = list(accumulate(upper)) + [0]
cnt = 0
for i in range(N-K):
    if upper[i+K-2] == upper[i-1]:
        cnt += 1
    elif P[i+K] == STmax.query(i+1, i+K+1) and P[i] == STmin.query(i, i+K):
        ans -= 1
if upper[N-2] == upper[N-K-1]:
    cnt += 1
print(ans - max(0, cnt-1))

Fixed submission:
import sys
from itertools import accumulate
class Segtree:
    def __init__(self, A, intv, initialize = True, segf = max):
        self.N = len(A)
        self.N0 = 2**(self.N-1).bit_length()
        self.intv = intv
        self.segf = segf
        if initialize:
            self.data = [intv]*self.N0 + A + [intv]*(self.N0 - self.N)
            for i in range(self.N0-1, 0, -1):
                self.data[i] = self.segf(self.data[2*i], self.data[2*i+1]) 
        else:
            self.data = [intv]*(2*self.N0)
        
    def update(self, k, x):
        k += self.N0
        self.data[k] = x
        while k > 0 :
            k = k >> 1
            self.data[k] = self.segf(self.data[2*k], self.data[2*k+1])
    
    def query(self, l, r):
        L, R = l+self.N0, r+self.N0
        s = self.intv
        while L < R:
            if R & 1:
                R -= 1
                s = self.segf(s, self.data[R])
            if L & 1:
                s = self.segf(s, self.data[L])
                L += 1
            L >>= 1
            R >>= 1
        return s
    
    def binsearch(self, l, r, check, reverse = False):
        L, R = l+self.N0, r+self.N0
        SL, SR = [], []
        while L < R:
            if R & 1:
                R -= 1
                SR.append(R)
            if L & 1:
                SL.append(L)
                L += 1
            L >>= 1
            R >>= 1
        
        if reverse:
            for idx in (SR + SL[::-1]):
                if check(self.data[idx]):
                    break
            else:
                return -1
            while idx < self.N0:
                if check(self.data[2*idx+1]):
                    idx = 2*idx + 1
                else:
                    idx = 2*idx
            return idx
        else:
            for idx in (SL + SR[::-1]):
                if check(self.data[idx]):
                    break
            else:
                return -1
            while idx < self.N0:
                if check(self.data[2*idx]):
                    idx = 2*idx
                else:
                    idx = 2*idx + 1
            return idx

readline = sys.stdin.readline
N, K = map(int, readline().split())
P = list(map(int, readline().split()))
STmax = Segtree(P, 0, True, max)
STmin = Segtree(P, 10**9, True, min)
ans = N - K + 1
upper = [0]*(N-1)
for i in range(N-1):
    if P[i] > P[i+1]:
        upper[i] = 1
upper = list(accumulate(upper)) + [0]
cnt = 0
for i in range(N-K):
    if upper[i+K-2] == upper[i-1]:
        cnt += 1
    elif P[i+K] == STmax.query(i+1, i+K+1) and P[i] == STmin.query(i, i+K):
        ans -= 1
if upper[N-2] == upper[N-K-1]:
    cnt += 1
print(ans - max(0, cnt-1))