Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
p = [int(i) for i in input().split()]
c = 0
for i, pi in enumerate(p):
    if pi == min(p[:i+1]):
        c += 1
print(c)

Fixed submission:
n = int(input())
p = [int(i) for i in input().split()]
c = 0
minp = p[0]
for pi in p:
    if pi < minp:
        minp = pi
        c += 1
print(c+1)
Buggy python submission (verdict: Runtime Error):
from itertools import permutations

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
for i, pi in enumerate(permutations(range(1, n+1))):
    if pi == p:
        a = i
    elif pi == q:
        b = i
print(abs(a - b))

Fixed submission:
from itertools import permutations

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
for i, pi in enumerate(permutations(range(1, n+1))):
    if pi == p:
        a = i
    if pi == q:
        b = i
print(abs(a - b))
Buggy python submission (verdict: Time Limit Exceeded):
n, k, q = map(int, input().split())
a = [int(input()) for _ in range(q)]
d = [a.count(i) for i in range(1, n + 1)]
for i in d:
    print("YNeos"[k - q + i < 1::2])

Fixed submission:
n, k, q = map(int, input().split())
d = dict()
for i in range(n):
    d[i + 1] = 0
for i in range(q):
    d[int(input())] += 1
for i in range(n):
    print("YNeos"[(q - k) >= d[i + 1]::2])

Buggy python submission (verdict: Time Limit Exceeded):
n, k, q = map(int, input().split())
a = [int(input()) for _ in range(q)]
for i in range(1, n + 1):
    print("YNeos"[k - q + a.count(i) < 1::2])

Fixed submission:
n, k, q = map(int, input().split())
d = dict()
for i in range(n):
    d[i + 1] = 0
for i in range(q):
    d[int(input())] += 1
for i in range(n):
    print("YNeos"[(q - k) >= d[i + 1]::2])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
h = [int(i) for i in input().split()]
for i in range(n - 1):
    if h[i] - 2 in h[i + 1:]:
        print("No")
        break
else:
    print("Yes")

Fixed submission:
n = int(input())
h = [int(i) for i in input().split()]
x = True
c = 0
for i in range(n):
    if i > 0 and h[i - 1] - h[i] >= 2:
        x = False
        break
    elif i > 0 and h[i - 1] - h[i] == 1:
        c += 1
    elif i > 0 and h[i] > h[i - 1] and c == 1:
        c = 0
    if c == 2:
        x = False
        break
if x:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
n, x = map(int, input().split())
l = [int(i) for i in input().split()]
b = 0
c = 1
for i in range(n + 1):
    if b + l[i] <= x:
        b += l[i]
        c += 1
    else:
        break
print(c)

Fixed submission:
n, x = map(int, input().split())
l = [int(i) for i in input().split()]
b = 0
c = 1
for i in range(n):
    if b + l[i] <= x:
        b += l[i]
        c += 1
    else:
        break
print(c)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
if m == 1:
    print(0)
else:
    x = [int(i) for i in input().split()]
    x.sort()
    d = x[- 1] - x[0]
    y = []
    for i in range(m - 1):
        y.append(x[i + 1] - x[i])
    y.sort(reverse=True)
    for i in range(n - 1):
        d -= y[i]
    print(d)

Fixed submission:
n, m = map(int, input().split())
if n >= m:
    print(0)
else:
    x = [int(i) for i in input().split()]
    x.sort()
    d = x[- 1] - x[0]
    y = []
    for i in range(m - 1):
        y.append(x[i + 1] - x[i])
    y.sort(reverse=True)
    for i in range(n - 1):
        d -= y[i]
    print(d)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(i) for i in input().split()]
b = 1
for i in a:
    b *= i
c = 0
while b % 2 == 0:
    b //= 2
    c += 1
print(c)

Fixed submission:
n = int(input())
a = [int(i) for i in input().split()]
b = 1
c = 0
for i in a:
    b = i
    while b % 2 == 0:
        b //= 2
        c += 1
print(c)
Buggy python submission (verdict: Runtime Error):
a = int(input())
b = int(input())
c = int(input())
d = int(input())
print(min(a, b) + min(c, d))

Fixed submission:
n = int(input())
d, x = map(int, input().split())
a = [int(input()) for i in range(n)]
c = 0
for i in range(n):
    for j in range(1, d + 1):
        if a[i] == 1:
            c += 1
        elif j % a[i] == 1:
            c += 1
print(c + x)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, inout().split())
if n * m == 1:
  print(1)
elif n == 1 or m == 1:
  print(n + m - 1)
else:
  print((n - 2) * (m - 2))

Fixed submission:
n, m = map(int, input().split())
if n * m == 1:
  print(1)
elif n == 1 or m == 1:
  print(n + m - 3)
else:
  print((n - 2) * (m - 2))
Buggy python submission (verdict: Runtime Error):
n = int(input())
for i in range(n ** 0.5 + 3):
  if i ** 2 > n:
    print((i - 1) ** 2)

Fixed submission:
n = int(input())
m = int(n ** 0.5 + 3)
for i in range(m):
    if i ** 2 > n:
        print((i - 1) ** 2)
        break

Buggy python submission (verdict: Runtime Error):
n = int(input())
m = n ** 0.5 + 3
for i in range(m,,-1):
  if i ** 2 < n:
    print(i ** 2)
    break

Fixed submission:
n = int(input())
m = int(n ** 0.5 + 3)
for i in range(m):
    if i ** 2 > n:
        print((i - 1) ** 2)
        break
Buggy python submission (verdict: Runtime Error):
s = input()
t = len(s)
print(s[0] + t + s[- 1])

Fixed submission:
s = input()
t = len(s) - 2
print(s[0] + str(t) + s[- 1])

Buggy python submission (verdict: Runtime Error):
s = input()
print(s[0] + (len(s) - 2) + s[- 1])

Fixed submission:
s = input()
t = len(s) - 2
print(s[0] + str(t) + s[- 1])
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = [int(i) for i in input().split()]
color = [0] * 8
any = 0
for i in a:
    if 1 <= i <= 399:
        color[0] = 1
    elif 400 <= i <= 799:
        color[1] = 1
    elif 800 <= i <= 1199:
        color[2] = 1
    elif 1200 <= i <= 1599:
        color[3] = 1
    elif 1600 <= i <= 1999:
        color[4] = 1
    elif 2000 <= i <= 2399:
        color[5] = 1
    elif 2400 <= i <= 2799:
        color[6] = 1
    elif 2800 <= i <= 3199:
        color[7] = 1
    else:
        any += 1
m = max(sum(color), 1)
M = min(sum(color) + any)
print(m, M)

Fixed submission:
n = int(input())
a = [int(i) for i in input().split()]
color = [0] * 8
any = 0
for i in a:
    if 1 <= i <= 399:
        color[0] = 1
    elif 400 <= i <= 799:
        color[1] = 1
    elif 800 <= i <= 1199:
        color[2] = 1
    elif 1200 <= i <= 1599:
        color[3] = 1
    elif 1600 <= i <= 1999:
        color[4] = 1
    elif 2000 <= i <= 2399:
        color[5] = 1
    elif 2400 <= i <= 2799:
        color[6] = 1
    elif 2800 <= i <= 3199:
        color[7] = 1
    else:
        any += 1
m = max(sum(color), 1)
M = sum(color) + any
print(m, M)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
SUM = 0
ans = 0

for i in range(n-1):
  for j in range(i+1, n):
    SUM += a[j]
  ans += a[i]*SUM
  SUM = 0

print(ans % (10**9 + 7))


Fixed submission:
n = int(input())
A = list(map(int, input().split()))
a = [0]
ans = 0

for i in range(n-1):
    a.append(a[i] + A[i])
for i in range(n):
    ans += A[i]*a[i]
print(ans % (10**9 + 7))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
ans = 0

for i in range(n-1):
  for j in range(i+1, n):
    ans += a[i] * a[j]

print(ans % (10**9 + 7))


Fixed submission:
n = int(input())
A = list(map(int, input().split()))
a = [0]
ans = 0

for i in range(n-1):
    a.append(a[i] + A[i])
for i in range(n):
    ans += A[i]*a[i]
print(ans % (10**9 + 7))
Buggy python submission (verdict: Runtime Error):
a, b, c = map(int, input().split())
k = int(input())

if a<b and b<c:
  print("Yse")

else:
  while k == 0:
    if a >= b:
      b *= 2
      k -= 1
    else:
      b >= c:
      c *= 2
      k -= 1
  
  if a<b and b<c:
    print("Yse")
  else:
    print("No")
    
    

Fixed submission:
a, b, c = map(int, input().split())
k = int(input())

if a<b and b<c:
  print("Yes")

else:
  while k != 0:
    if a >= b:
      b *= 2
      k -= 1
    else:
      c *= 2
      k -= 1
  
  if a<b and b<c:
    print("Yes")
  else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = []

for i in range(n-k+1):
  m = 1
  for j in range(k):
    m *= a[j]
  s.append(m)
  a.pop(0)

for i in range(n-k):
  if s[i] < s[i+1]:
    print("Yes")
  else:
    print("No")


Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

for i in range(n-k):
  if a[i] < a[i + k]:
      print("Yes")
  else:
      print("No")

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = ["Yes"]*(n-k)
s = []

for i in range(n-k+1):
  m = 1
  for j in range(k):
    m *= a[j]
  s.append(m)
  a.pop(0)

for i in range(n-k):
  if s[i] >= s[i+1]:
    ans[i] = "No"

for i in range(len(ans)):
    print(ans[i])


Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

for i in range(n-k):
  if a[i] < a[i + k]:
      print("Yes")
  else:
      print("No")
Buggy python submission (verdict: Runtime Error):
x ,n = map(int, input().split())
s = list(map(int, input().split()))
number = list(range(0,102))
min = 100
if n == 0:
  print(x)
  exit()
  
for i in range(n):
  number.remove(s[i])

for i in range(100 - n):
  if min > abs(x - number[i]):
    min = abs(x - number[i])
    j = i
print(number[j])


Fixed submission:
x ,n = map(int, input().split())
s = list(map(int, input().split()))
number = list(range(0,102))
min = 100
if n == 0:
  print(x)
  exit()
  
for i in range(n):
  number.remove(s[i])

for i in range(102 - n):
  if min > abs(x - number[i]):
    min = abs(x - number[i])
    j = i
print(number[j])

Buggy python submission (verdict: Runtime Error):
x ,n = map(int, input().split())
s = list(map(int, input().split()))
number = list(range(1,101))
min = 100
if n == 0:
  print(x)
  exit()
  
for i in range(n):
  number.remove(s[i])

for i in range(100 - n):
  if min > abs(x - number[i]):
    min = abs(x - number[i])
    j = i
print(number[j])

Fixed submission:
x ,n = map(int, input().split())
s = list(map(int, input().split()))
number = list(range(0,102))
min = 100
if n == 0:
  print(x)
  exit()
  
for i in range(n):
  number.remove(s[i])

for i in range(102 - n):
  if min > abs(x - number[i]):
    min = abs(x - number[i])
    j = i
print(number[j])
Buggy python submission (verdict: Runtime Error):
n = input()

if n[lne(n) - 1] == '3':
  print('bon')
elif n[len(n) - 1] == '0' or '1' or '6' or '8':
  print('pon')
else:
  print('hon')

Fixed submission:
n = int(input())

if n%10 in [2, 4, 5, 7, 9]:
  print('hon')
elif n%10 in [0, 1, 6, 8]:
  print('pon')
else:
  print('bon')
Buggy python submission (verdict: Runtime Error):
N = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(N):
  if p[i] != i+1:
    ans += 1

if ans <= 2:
  print("YES")
else:
  prin("NO")

Fixed submission:
N = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(N):
  if p[i] != i+1:
    ans += 1

if ans <= 2:
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Runtime Error):
 = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0

for i in range(n):
  if a[i] < b[i]:
    ab = b[i] - a[i]
    if a[i+1] > ab:
      ans += b[i]
      a[i+1] -= ab
    else:
      ans += a[i] + a[i+1]
      a[i+1] = 0
  else:
    ans += b[i]

print(ans)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0

for i in range(n):
  if a[i] < b[i]:
    ab = b[i] - a[i]
    if a[i+1] > ab:
      ans += b[i]
      a[i+1] -= ab
    else:
      ans += a[i] + a[i+1]
      a[i+1] = 0
  else:
    ans += b[i]

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
N, X = map(int, input().split())
L = list(map(int, input().split()))
cnt = 0
total = 0
for i in range(N+1):
  if total <= X:
    cnt += 1
    total += L[i]
  else:
    print(cnt)
    sys.exit()
print(N+1)


Fixed submission:

N, X = map(int, input().split())
L = list(map(int, input().split()))
cnt = 1
total = 0
for i in range(N):
  total += L[i]
  if total <= X:
    cnt += 1
  else:
    break
print(cnt)

Buggy python submission (verdict: Runtime Error):
import sys
N, X = map(int, input().split())
L = list(map(int, input().split()))
cnt = 0
total = 0
for i in range(N+1):
  if total <= X:
    cnt += 1
    total += L[i]
  else:
    print(cnt)
    sys.exit()

Fixed submission:

N, X = map(int, input().split())
L = list(map(int, input().split()))
cnt = 1
total = 0
for i in range(N):
  total += L[i]
  if total <= X:
    cnt += 1
  else:
    break
print(cnt)
Buggy python submission (verdict: Runtime Error):
K, A, B = map(int, input().split())
count = 1
coin = 0
if B - A <= 2:
  print(K + 1)
  
else:
  for i in range(K):
    if coin == 1:
      count += B
      coin -= 1
    elif count >= A and i != k-1:
      count -= A
      coin += 1
    else:
      count += 1
  print(count)
  
  

Fixed submission:
K, A, B = map(int, input().split())
ans = 0
if B - A <= 2:
  print(K + 1)

else:
  K -= A - 1
  ans += (K//2) * (B - A) + A
  ans += K % 2
  print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ans = 1
for i in range(1, n+1):
  ans *= i
  
ans %= 10**9 + 7
print(ans)


Fixed submission:
n = int(input())
ans = 1
for i in range(1, n+1):
  ans *= i
  if ans >= 10**9 + 7:
    ans %= 10**9 + 7
print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
ans = 1
for i in range(1, n+1):
  ans *= i
print(ans/(10**9+7))

Fixed submission:
n = int(input())
ans = 1
for i in range(1, n+1):
  ans *= i
  if ans >= 10**9 + 7:
    ans %= 10**9 + 7
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
ans = 0
for i in range(k+1):
  for j in range(k+1):
    for l in range(k+1):
      if (i + j + l) == s:
        ans += 1
        
print(ans)

Fixed submission:
k, s = map(int, input().split())
ans = 0
for i in range(k+1):
  for j in range(k+1):
    z = (s - i - j)
    if 0<= z <= k:
      ans += 1
        
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
a, b, x = map(int,input().split())
count = 0
for i in range(a,b+1):
  if i % x == 0:
    count+=1
print(count)

Fixed submission:
a, b, x = map(int,input().split())

A = (a-1)//x
B = b//x

print(B - A)
Buggy python submission (verdict: Runtime Error):
n, k, x, y = map(int, [input for i in range(4)])

print(x*k + y*(n-k))

Fixed submission:
n, k, x, y = map(int, [input() for i in range(4)])

if n <= k:
  print(x*n)
else:
  print(x*k + y*(n-k))
Buggy python submission (verdict: Runtime Error):
s = list(map(int, input().split()))

for i in s:
  if i == 5:
    count5+=1
  else:
    count7+=1

if count5 == 2 and count7 == 1:
  print("YES")

else:
  print("NO")
    

Fixed submission:
s = list(map(int, input().split()))
count5 = 0
count7 = 0
for i in s:
  if i == 5:
    count5+=1
  else:
    count7+=1

if count5 == 2 and count7 == 1:
  print("YES")

else:
  print("NO")
Buggy python submission (verdict: Runtime Error):
if int(inout())%9==0:
  print("Yes")
else:
  print("No")

Fixed submission:
if int(input())%9==0:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main(){
  char c;cin>>c;
  if(65 <= int(c) && int(c) <= 90) cout<<'A'<<endl;
  else cout<<'a'<<endl;
}


Fixed submission:
n = input()

if (n.isupper()):
   print('A')
else:
  print('a')
Buggy python submission (verdict: Runtime Error):
#python
for _ in range(int(input())):
	n1,n2=map(int,input().split())
	if n1==n2:
		c+=1
		m=max(m,c)
	else:
		m=max(m,c)
		c=0
if m>=3:
	print('Yes')
else:
	print('No')

Fixed submission:
n=int(input())
m=0
c=0
for _ in range(n):
	n1,n2=map(int,input().split())
	if n1==n2:
		c+=1
		m=max(m,c)
	else:
		m=max(m,c)
		c=0
if m>=3:
	print('Yes')
else:
	print('No')

Buggy python submission (verdict: Runtime Error):
for _ in range(int(input())):
	n1,n2=map(int,input().split())
	if n1==n2:
		c+=1
		m=max(m,c)
	else:
		m=max(m,c)
		c=0
if m>=3:
	print('Yes')
else:
	print('No')

Fixed submission:
n=int(input())
m=0
c=0
for _ in range(n):
	n1,n2=map(int,input().split())
	if n1==n2:
		c+=1
		m=max(m,c)
	else:
		m=max(m,c)
		c=0
if m>=3:
	print('Yes')
else:
	print('No')

Buggy python submission (verdict: Runtime Error):
for _ in range(int(input())):
	n1,n2=map(int,input().split())
	if n1==n2:
		c+=1
		m=max(m,c)
	else:
		m=max(m,c)
		c=0
	#print(c)
if m>=3:
	print('Yes')
else:
	print('No')

Fixed submission:
n=int(input())
m=0
c=0
for _ in range(n):
	n1,n2=map(int,input().split())
	if n1==n2:
		c+=1
		m=max(m,c)
	else:
		m=max(m,c)
		c=0
if m>=3:
	print('Yes')
else:
	print('No')
Buggy python submission (verdict: Time Limit Exceeded):
k_num, s_num = map(int, input().split())

cnt = 0
for x in range(k_num+1):
    for y in range(k_num+1):
        for z in range(k_num+1):
            if x + y + z == s_num:
                cnt += 1

print(cnt)

Fixed submission:
k_num, s_num = map(int, input().split())

cnt = 0
for x in range(k_num+1):
    l = s_num - x
    for y in range(k_num+1):
        z = l - y
        if 0 <= z <= k_num:
            cnt += 1

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
k_num, s_num = map(int, input().split())

cnt = 0
for x in range(k_num+1):
    for y in range(k_num+1):
        for z in range(k_num+1):
            if x + y + z == s_num:
                cnt += 1

print(cnt)

Fixed submission:
k_num, s_num = map(int, input().split())

cnt = 0
for x in range(k_num+1):
    l = s_num - x
    for y in range(k_num+1):
        z = l - y
        if 0 <= z <= k_num:
            cnt += 1

print(cnt)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
ABs = [tuple(map(int, input().split())) for _ in range(M)]

par = [i for i in range(N)]

def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]

def same(x, y):
    return find(x) == find(y)

def unite(x, y):
    x = find(x)
    y = find(y)
    if x != y:
        par[x] = y

for a, b in ABs:
    unite(a-1, b-1)

print(len(set(par) -1))


Fixed submission:
N, M = map(int, input().split())
ABs = [tuple(map(int, input().split())) for _ in range(M)]

par = [i for i in range(N)]

def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]

def unite(x, y):
    x = find(x)
    y = find(y)
    if x != y:
        par[x] = y

for a, b in ABs:
    unite(a-1, b-1)

for x in range(N):
    find(x)

print(len(set(par))-1)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
LRs = [tuple(map(int, input().split())) for _ in range(K)]
mod = 998244353
    
mem = [0] * (N)
mem[N-1] = 1

for _i in range(N-1):
    i = N-2 - _i
    for l, r in LRs:
        if i+l < N:
            mem[i] += sum(mem[i+l:min(i+r+1, N)])

    mem[i] %= mod

print(mem[0])

Fixed submission:
N, K = map(int, input().split())
LRs = [tuple(map(int, input().split())) for _ in range(K)]
mod = 998244353

def diff(l):
    tmp = None
    res = []
    for elm in l:
        if tmp is None:
            res.append(elm)
            tmp = elm
        else:
            res.append(elm-tmp)
            tmp = elm

    return res

res_diff = [0] * N
res_diff[1] = -1
res = [1]

for i in range(N-1):
    for l, r in LRs:
        if i+l < N:
            res_diff[i+l] += res[i]
        if i+r+1 < N:
            res_diff[i+r+1] -= res[i]
    
    res_next = (res[-1]+res_diff[i+1])%mod
    res.append(res_next)

print(res[N-1])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

N, K = map(int, input().split())
LRs = [tuple(map(int, input().split())) for _ in range(K)]
mod = 998244353

dd = defaultdict(int)
dd[N] = 1

for _i in range(N-1):
    i = N-1 - _i
    for l, r in LRs:
        for d in range(l, r+1):
            dd[i] = (dd[i] + dd[i+d]) % mod

print(dd[1])



Fixed submission:
N, K = map(int, input().split())
LRs = [tuple(map(int, input().split())) for _ in range(K)]
mod = 998244353

def diff(l):
    tmp = None
    res = []
    for elm in l:
        if tmp is None:
            res.append(elm)
            tmp = elm
        else:
            res.append(elm-tmp)
            tmp = elm

    return res

res_diff = [0] * N
res_diff[1] = -1
res = [1]

for i in range(N-1):
    for l, r in LRs:
        if i+l < N:
            res_diff[i+l] += res[i]
        if i+r+1 < N:
            res_diff[i+r+1] -= res[i]
    
    res_next = (res[-1]+res_diff[i+1])%mod
    res.append(res_next)

print(res[N-1])
Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
rcx = [tuple(map(int, input().split())) for _ in range(Q)]

thres_1, thres_2 = N, N
nearest_white_1 = [N] * N
nearest_white_2 = [N] * N

reverse_count = 0
for rc, x in rcx:
    if rc == 1:
        if x < thres_1:
            reverse_count += thres_2 - 2
            for i in range(1, thres_2):
                nearest_white_2[i] = x
            thres_1 = x
        else:
            reverse_count += nearest_white_1[x] - 2
    else:
        if x < thres_2:
            reverse_count += thres_1 - 2
            for i in range(1, thres_1):
                nearest_white_1[i] = x
            thres_2 = x
        else:
            reverse_count += nearest_white_2[x] - 2

    #print(reverse_count)
    #print(f"thres1:{thres_1} {nearest_white_1}")
    #print(f"thres2:{thres_2} {nearest_white_2}")
    #print('-'*20)

print((N-2)**2 - reverse_count)

Fixed submission:
N, Q = map(int, input().split())
rcx = [tuple(map(int, input().split())) for _ in range(Q)]

thres_1, thres_2 = N-2, N-2
nearest_white_1 = [0] * (N-2)
nearest_white_2 = [0] * (N-2)

reverse_count = 0
for rc, x in rcx:
    x = x-2
    if rc == 1:
        if x < thres_1:
            for i in range(x+1, thres_1):
                nearest_white_1[i] = thres_2
            thres_1 = x
            reverse_count += thres_2
        else:
            reverse_count += nearest_white_1[x]
    else:
        if x < thres_2:
            for i in range(x+1, thres_2):
                nearest_white_2[i] = thres_1
            reverse_count += thres_1
            thres_2 = x
        else:
            reverse_count += nearest_white_2[x]

    #print(reverse_count)
    #print(f"thres1:{thres_1} {nearest_white_1}")
    #print(f"thres2:{thres_2} {nearest_white_2}")
    #print('-'*20)

print((N-2)**2 - reverse_count)