Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
def main():
    s=input()
    string=('dream','erase')

    i=0
    n=len(s)
    while i<n:
        st=s[i:i+5]
        if st==string[0]:
            i+=5
            st=s[i:i+2]
            if st=='er':
                if s[i+2]!='a':
                    i+=2
        
        elif st==string[1]:
            i+=5
            if i==n:
              break
            if s[i]=='r':
                i+=1
        
        else:
            print('NO')
            exit()
    print('YES')
if  __name__=='__main__':
    main()

Fixed submission:
def main():
    s=input()
    string=('dream','erase')

    i=0
    n=len(s)
    while i<n:
        st=s[i:i+5]
        if st==string[0]:
            i+=5
            st=s[i:i+2]
            if st=='er':
                if i+2==n:
                    break
                if s[i+2]!='a':
                    i+=2
        
        elif st==string[1]:
            i+=5
            if i==n:
                break
            if s[i]=='r':
                i+=1
        
        else:
            print('NO')
            exit()
    print('YES')
if  __name__=='__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    s=input()
    string=('dream','erase')

    i=0
    n=len(s)
    while i<n:
        st=s[i:i+5]
        if st==string[0]:
            i+=5
            st=s[i:i+2]
            if st=='er':
                if s[i+2]!='a':
                    i+=2
        
        elif st==string[1]:
            i+=5
            if s[i]=='r':
                i+=1
        
        else:
            print('NO')
            exit()
    print('YES')
if  __name__=='__main__':
    main()

Fixed submission:
def main():
    s=input()
    string=('dream','erase')

    i=0
    n=len(s)
    while i<n:
        st=s[i:i+5]
        if st==string[0]:
            i+=5
            st=s[i:i+2]
            if st=='er':
                if i+2==n:
                    break
                if s[i+2]!='a':
                    i+=2
        
        elif st==string[1]:
            i+=5
            if i==n:
                break
            if s[i]=='r':
                i+=1
        
        else:
            print('NO')
            exit()
    print('YES')
if  __name__=='__main__':
    main()
Buggy python submission (verdict: Runtime Error):
x=list(input())
n=len(x)

stack=[x[0]]
for i in range(1,n):
  if x[i]=='S':
    stack.append('S')
  else:
    if stack[-1]=='S':
      stack.pop()
    else:
      stack.append('T')
print(len(stack))

Fixed submission:
x=list(input())
n=len(x)

stack=['T',x[0]]
for i in range(1,n):
  if x[i]=='S':
    stack.append('S')
  else:
    if stack[-1]=='S':
      stack.pop()
    else:
      stack.append('T')
print(len(stack)-1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
INF = 10**10
MOD = 10**9 + 7
sys.setrecursionlimit(100000000)
from functools import lru_cache
from itertools import permutations
from math import factorial


def main():
    n,a = map(int,input().split())
    x = list(map(int,input().split()))
    dp = [[[0] * (n * a + 1) for _ in range(n + 1)] for _ in range(n + 1)]

    for i in range(n + 1):
        dp[i][0][0] = 1

    for i in range(n):
        num = x[i]
        for j in range(1,n + 1):
            for s in range(n * a + 1):
                if s - num >= 0:
                    dp[i + 1][j][s] = dp[i][j][s] + dp[i][j - 1][s - num]
                else:
                    dp[i + 1][j][s] = dp[i][j][s]
    
    ans = 0
    for i in range(1,n + 1):
        ans += dp[-1][i][i * a]
    print(ans)
    

if __name__=='__main__':
    main() 

Fixed submission:
import sys
input = sys.stdin.readline
INF = 10**10
MOD = 10**9 + 7
sys.setrecursionlimit(100000000)
from functools import lru_cache
from itertools import permutations
from math import factorial


def main():
    n,a = map(int,input().split())
    x = list(map(int,input().split()))
    dp = [[[0] * (n * a + 1) for _ in range(n + 1)] for _ in range(n + 1)]

    for i in range(n + 1):
        dp[i][0][0] = 1

    for i in range(n):
        num = x[i]
        for j in range(1,n + 1):
            for s in range(n * a + 1):
                if s - num >= 0:
                    dp[i + 1][j][s] = dp[i][j][s] + dp[i][j - 1][s - num]
                else:
                    dp[i + 1][j][s] = dp[i][j][s]
    
    ans = 0
    for i in range(1,n + 1):
        ans += dp[-1][i][i * a]
    print(ans)
    

if __name__=='__main__':
    main()
Buggy python submission (verdict: Runtime Error):
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す．
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]


# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n = int(readline())
xyi = []

for i in range(n):
    x,y = map(int,readline().split())
    xyi.append((x,y,i))

UF = UnionFind(n)
xyi.sort(key = lambda p:p[0])
#print(xyi)

q = []
for x,y,i in xyi:
    print(q)
    if q and q[-1][0] < y: Y,I = q[-1]
    else: Y,I = y,i
    while q and q[-1][0] < y:
        yy,ii = q.pop()
        UF.merge(i,ii)
    q.append((Y,I))
    #print(i,idx)

for i in range(n):
    print(UF.getsize(i))



Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す．
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]


# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n = int(readline())
xyi = []

for i in range(n):
    x,y = map(int,readline().split())
    xyi.append((x,y,i))

UF = UnionFind(n)
xyi.sort(key = lambda p:p[0])
#print(xyi)

q = []
for x,y,i in xyi:
    #print(q)
    if q and q[-1][0] < y: Y,I = q[-1]
    else: Y,I = y,i
    while q and q[-1][0] < y:
        yy,ii = q.pop()
        UF.merge(i,ii)
    q.append((Y,I))
    #print(i,idx)

for i in range(n):
    print(UF.getsize(i))
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n, = map(int,readline().split())
*a, = map(int,readline().split())
*b, = map(int,readline().split())
b = b[::-1]

from collections import Counter
d = Counter(a+b)
if max(d.values()) > n:
    print("No")
    exit()

same = set(i for i in range(n) if a[i]==b[i])
from random import randrange
while same:
    i = same.pop()
    while True:
        j = randrange(0,n)
        if a[j] != b[i] and a[i] != b[j]:
            b[i],b[j] = b[j],b[i]
            same.discard(j)
            break

print("Yes")
print(*b)


Fixed submission:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

x, = map(int,read().split())
print(1-x)
Buggy python submission (verdict: Time Limit Exceeded):
SIZE=2*10**6; MOD=10**9+7 #998244353 #ここを変更する

SIZE += 1
inv = [0]*SIZE  # inv[j] = j^{-1} mod MOD
fac = [0]*SIZE  # fac[j] = j! mod MOD
finv = [0]*SIZE # finv[j] = (j!)^{-1} mod MOD
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE):
    fac[i] = fac[i-1]*i%MOD
finv[-1] = pow(fac[-1],MOD-2,MOD)
for i in range(SIZE-1,0,-1):
    finv[i-1] = finv[i]*i%MOD
    inv[i] = finv[i]*fac[i-1]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0



# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

k, = map(int,readline().split())
s = input()

MOD = 10**9+7

l = len(s)
ans = pow(26,l+k,MOD)
for p in range(l):
    ans -= pow(25,k+l-p)*choose(l+k,p)

print(ans%MOD)






Fixed submission:
SIZE=2*10**6; MOD=10**9+7 #998244353 #ここを変更する

SIZE += 1
inv = [0]*SIZE  # inv[j] = j^{-1} mod MOD
fac = [0]*SIZE  # fac[j] = j! mod MOD
finv = [0]*SIZE # finv[j] = (j!)^{-1} mod MOD
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE):
    fac[i] = fac[i-1]*i%MOD
finv[-1] = pow(fac[-1],MOD-2,MOD)
for i in range(SIZE-1,0,-1):
    finv[i-1] = finv[i]*i%MOD
    inv[i] = finv[i]*fac[i-1]%MOD

def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0



# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

k, = map(int,readline().split())
s = input()

MOD = 10**9+7

l = len(s)
ans = pow(26,l+k,MOD)

p25 = [1]
for _ in range(SIZE):
    p25.append(p25[-1]*25%MOD)

for p in range(l):
    ans -= p25[k+l-p]*choose(l+k,p)%MOD

print(ans%MOD)
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,s,*a = map(int,read().split())

MOD = 998244353
def mul(c,m,dp): #multiply (2+x^c) up to x^m
    for i in range(m,c-1,-1):
        dp[i] *= 2
        dp[i] += dp[i-c]
        dp[i] %= MOD
    for i in range(c-1,-1,-1):
        dp[i] *= 2
        dp[i] %= MOD

    
dp = [0]*(s+1)
dp[0] = 1

for i in a:
    mul(i,s,dp)

print(dp[-1]%MOD)
#print(dp)





Fixed submission:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,s,*a = map(int,read().split())

MOD = 998244353
def mul(c,m,dp): #multiply (2+x^c) up to x^m
    for i in range(m,c-1,-1):
        dp[i] *= 2
        dp[i] += dp[i-c]
        dp[i] %= MOD
    for i in range(min(m,c-1),-1,-1):
        dp[i] *= 2
        dp[i] %= MOD

    
dp = [0]*(s+1)
dp[0] = 1

for i in a:
    mul(i,s,dp)

print(dp[-1]%MOD)
#print(dp)
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline 

n,k,*a = list(map(int,read().split()))
for i in range(n): a[i] -= 1

def pow2(a,n):
    if n==0: return list(range(n))
    if n==1: return a
    if n%2:
        return mul(a,pow2(a,n-1))
    else:
        x = pow2(a,n//2)
        return mul(x,x)

def mul(a,b):
    return [a[bi] for bi in b]

x = pow2(a,k)
print(x[0]+1)

Fixed submission:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline 


n,k,*a = list(map(int,read().split()))
for i in range(n): a[i] -= 1


now = 0
cnt = 0
used = [-1]*n

while used[now] == -1:
    used[now] = cnt
    cnt += 1
    now = a[now]
    if cnt == k:
        print(now+1)
        exit()

c = cnt - used[now]
#print(used,cnt,c,now)

k -= used[now]
k %= c
print(used.index(k+used[now])+1)
Buggy python submission (verdict: Runtime Error):
n,*a = map(int,input().split())
s = [[*input()] for _ in range(n)]
a = {p:q for p,q in zip("ABC",a)}
ans = ["Yes"]+[0]*n
def dfs(i):
	if i==n:
	    print("\n".join(ans))
	    exit()
	for p,m in [s[i],s[i][::-1]]:
		if a[p]:
		    a[p]+=1; a[m]-=1; ans[i+1]=p
		    dfs(i+1)
		    a[p]-=1; a[m]+=1
dfs(0)
print("No")

Fixed submission:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

#n = int(input())
n,*a = [int(i) for i in readline().split()]
s = read().split()

ans = ""
ok = 1

d = {"AB":0,"BC":1,"AC":2}
d2 = "ABC"

if sum(a) >= 3 or sum(a) <= 1:
    for i in s:
        v = d[i]
        w = (v+1)%3
        if a[w] < a[v]: v,w = w,v
        a[v] += 1
        a[w] -= 1
        ans += d2[v]
        if a[w] < 0: ok = 0
    
        #print(i,a,d2[v])
    if ok:
        print("Yes")
        print(*ans,sep="\n")
    else:
        print("No")


#d = {"AB":0,"BC":1,"AC":2}
#d2 = "ABC"

else: # (1,1,0) or (2,0,0)型
    for idx,c in enumerate(s):
        v = d[c]
        w = (v+1)%3
        
        if a[v]==1 and a[w]==1 and idx<n-1 and s[idx+1] != c:#先読み
            vv = d[s[idx+1]]
            ww = (vv+1)%3
            r = [0]*3
            r[v] += 1; r[w] += 1; r[vv] += 1; r[ww] += 1
            vv = r.index(2)
            ww = (vv+1)%3 if vv==v else (vv-1)%3
            a[vv] += 1
            a[ww] -= 1
            ans += d2[vv]
            if a[ww] < 0: ok = 0
        else: 
            if a[w] < a[v]: v,w = w,v
            a[v] += 1
            a[w] -= 1
            ans += d2[v]
            if a[w] < 0: ok = 0
        
        #print(a,c,ans[-1])
        
    if ok:
        print("Yes")
        print(*ans,sep="\n")
    else:
        print("No")
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read


s = input()

MOD = 2019
d = [0]*MOD
d[0] = 1
r = 0
t = 1
for i in reversed(s):
    r += int(i)*t
    r %= MOD
    t *= 10
    d[r] += 1
    
print(sum(i*(i-1)//2 for i in d))

    





Fixed submission:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read


s = input()

MOD = 2019
d = [0]*MOD
d[0] = 1
r = 0
t = 1
for i in reversed(s):
    r += int(i)*t
    r %= MOD
    t *= 10
    t %= MOD
    d[r] += 1
    
print(sum(i*(i-1)//2 for i in d))
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read


#n,*a = [int(i) for i in read().split()]

h,w,k = [int(i) for i in readline().split()]
b = [readline() for _ in range(h)]

ans = INF = 10**9
N = 2**(h-1)


for mask in range(N):
    res = [(mask>>j)&1 for j in range(h-1)]
    
    R = []
    now = [0]
    for i in range(h-1):
        if res[i] == 0:
            now.append(i+1)
        else:
            R.append(now[:])
            now = [i+1]
    if now: R.append(now[:])
    
    L = len(R)
    val = res.count(1)

    num = [0]*L
    for i in range(w):
        nnum = [0]*L
        for j in range(L):
            p = 0
            for kk in R[j]:
                if b[kk][i]=="1": p += 1
            nnum[j] = p
        
        #print(R,i,num,nnum)
        for j in range(L):   
            if num[j] + nnum[j] > k:
                val += 1
                num = nnum
                break
        else:
            for j in range(L): num[j] += nnum[j]
        
        
        if any(ni>k for ni in nnum):
            val = INF
            break
    
    #print(val)
    ans = min(ans,val)

print(ans)






    

Fixed submission:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read


#n,*a = [int(i) for i in read().split()]

h,w,k = [int(i) for i in readline().split()]
b = [readline() for _ in range(h)]

ans = INF = 10**9
N = 2**(h-1)


for mask in range(N):
    res = [(mask>>j)&1 for j in range(h-1)]
    
    R = []
    now = [0]
    for i in range(h-1):
        if res[i] == 0:
            now.append(i+1)
        else:
            R.append(now[:])
            now = [i+1]
    if now: R.append(now[:])
    
    L = len(R)
    val = res.count(1)

    num = [0]*L
    for i in range(w):
        nnum = [0]*L
        for j in range(L):
            p = 0
            for kk in R[j]:
                if b[kk][i]=="1": p += 1
            nnum[j] = p
        
        #print(R,i,num,nnum)
        for j in range(L):   
            if num[j] + nnum[j] > k:
                val += 1
                num = nnum
                break
        else:
            for j in range(L): num[j] += nnum[j]
        
        
        if any(ni>k for ni in nnum):
            val = INF
            break
    
    #print(val)
    ans = min(ans,val)

print(ans)
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,m = map(int,readline().split())
ab = [tuple(map(int,readline().split())) for _ in range(n)]
lr = [tuple(map(int,readline().split())) for _ in range(m)]

ab.sort()
a = []
b = []
for i,j in ab:
    a.append(i)
    b.append(j)

from bisect import *
g = [[] for _ in range(n+1)]
lidx = []
ridx = []
for i in range(m):
    l,r = lr[i]
    li = bisect_left(a,l)
    ri = bisect_right(a,r)
    if li != ri:
        lidx.append(li)
        ridx.append(ri)
        g[li].append((ri,i+1))
        g[ri].append((li,i+1))


b.append(0)
for i in range(n-1,-1,-1):
    b[i+1] ^= b[i]

#print(b)
# imos したので、lidx に +1, ridx に -1 と同じ
q = [0]
parent = [-1]*(n+1)
edge_idx = [-1]*(n+1)
order = []
for i in range(n):
    if parent[i] == -1:
        q = [i]
        while q:
            v = q.pop()
            order.append(v)
            if parent[v] == -1: parent[v] = -2
            for c,idx in g[v]:
                if parent[c] == -1:
                    parent[c] = v
                    edge_idx[c] = idx
                    q.append(c)

"""
print(g)
print(lidx)
print(ridx)
print(b,"b")
print(parent,"parent")
print(edge_idx,"e_idx")
print(order)
"""
#print(b)
ans = []    
for i in order[::-1]:
    if b[i] and parent[i] != -2:
        b[i] = 0
        b[parent[i]] ^= 1
        ans.append(edge_idx[i])
    #print(b,i)
    
#print(b)

if all(not i for i in b):
    print(len(ans))
    print(*sorted(ans))
else:
    print(-1)

assert n!=3 and m!=4




Fixed submission:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,m = map(int,readline().split())
ab = [tuple(map(int,readline().split())) for _ in range(n)]
lr = [tuple(map(int,readline().split())) for _ in range(m)]

ab.sort()
a = []
b = []
for i,j in ab:
    a.append(i)
    b.append(j)

from bisect import *
g = [[] for _ in range(n+1)]
lidx = []
ridx = []
for i in range(m):
    l,r = lr[i]
    li = bisect_left(a,l)
    ri = bisect_right(a,r)
    if li != ri:
        lidx.append(li)
        ridx.append(ri)
        g[li].append((ri,i+1))
        g[ri].append((li,i+1))


b.append(0)
for i in range(n-1,-1,-1):
    b[i+1] ^= b[i]

#print(b)
# imos したので、lidx に +1, ridx に -1 と同じ
q = [0]
parent = [-1]*(n+1)
edge_idx = [-1]*(n+1)
order = []
for i in range(n,-1,-1):
    if parent[i] == -1:
        q = [i]
        while q:
            v = q.pop()
            order.append(v)
            if parent[v] == -1: parent[v] = -2
            for c,idx in g[v]:
                if parent[c] == -1:
                    parent[c] = v
                    edge_idx[c] = idx
                    q.append(c)

"""
print(g)
print(lidx)
print(ridx)
print(b,"b")
print(parent,"parent")
print(edge_idx,"e_idx")
print(order)
"""

ans = []    
for i in order[::-1]:
    if b[i] and parent[i] != -2:
        b[i] = 0
        b[parent[i]] ^= 1
        ans.append(edge_idx[i])
    #print(b,i)
    
#print(b)

if all(not i for i in b[:-1]):
    print(len(ans))
    print(*sorted(ans))
else:
    print(-1)
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

#n,*w = [int(i) for i in read().split()]
#n,*t = map(int,read().split())

#n = int(input())
#s = input()
h,n = [int(i) for i in readline().split()]

 
def div(m,a,b,dp): #divide by (1+bx^a) up to x^m
    for i in range(m-a+1):
        dp[i+a] = min(dp[i+a], dp[i]+b)
    for i in range(m-a+1,m):
        dp[-1] = min(dp[-1],dp[i]+b)
        #dp[i+c] += dp[i]


INF = 10**9+7
dp =[INF]*(h+1)
dp[0] = 0

for _ in range(n):
    a,b = [int(i) for i in readline().split()]
    div(h,a,b,dp)
    #print(a,b,dp)
 


print(dp[-1])


Fixed submission:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

#n,*w = [int(i) for i in read().split()]
#n,*t = map(int,read().split())

#n = int(input())
#s = input()
h,n = [int(i) for i in readline().split()]

 
def div(m,a,b,dp): #divide by (1+bx^a) up to x^m
    for i in range(m-a+1):
        dp[i+a] = min(dp[i+a], dp[i]+b)
    for i in range(max(m-a+1,0),m):
        dp[-1] = min(dp[-1],dp[i]+b)
        #dp[i+c] += dp[i]


INF = 10**9+7
dp =[INF]*(h+1)
dp[0] = 0

for _ in range(n):
    a,b = [int(i) for i in readline().split()]
    div(h,a,b,dp)
    #print(a,b,dp)
 


print(dp[-1])
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

n = int(input())
p = tuple([int(i) for i in readline().split()])
q = tuple([int(i) for i in readline().split()])


from itertools import permutations
L = list(range(1,n+1))
for i,c in enumerate(permutations(L)):
    if c == p:
        a = i
    elif c == q:
        b = i

print(abs(a-b))










Fixed submission:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

n = int(input())
p = tuple([int(i) for i in readline().split()])
q = tuple([int(i) for i in readline().split()])

if p == q:
    print(0)
    exit()

from itertools import permutations
L = list(range(1,n+1))
for i,c in enumerate(permutations(L)):
    if c == p:
        a = i
    elif c == q:
        b = i

print(abs(a-b))
Buggy python submission (verdict: Runtime Error):

# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline 
readlines = sys.stdin.readlines

#n = int(input())
from math import gcd

n,x,d = [int(i) for i in readline().split()]

g = gcd(x,d) 

if d < 0:
    x = x+(n-1)*d
    d = -d

if d == 0:
    if x == 0: print(1)
    else: print(n+1)
    exit()

kind = d//g #あまりは kind = d//g 種類 

ans = 0


for i in range(kind):
    res = []
    for j in range(i,n+1,kind):
        res.append([j*x+j*(j-1)//2*d, j*x+(j*n-j*(j+1)//2)*d])
    
    if not res: continue
    
    #print(res)
    res.sort()
    
    q = [res[0][:]]

    for e,f in res:
        while q:
            a,b = q[-1]
            if b < e: break
            else:
                q.pop()
                e,f = a,max(b,f)
        
        q.append([e,f])
    
    #print(q)
    for a,b in q:
        ans += (b-a)//d + 1


print(ans)









Fixed submission:

# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline 
readlines = sys.stdin.readlines

#n = int(input())
from fractions import gcd

n,x,d = [int(i) for i in readline().split()]

g = gcd(x,d) 

if d < 0:
    x = x+(n-1)*d
    d = -d

if d == 0:
    if x == 0: print(1)
    else: print(n+1)
    exit()

kind = abs(d//g) #あまりは kind = d//g 種類 

ans = 0



for i in range(min(kind,n+2)):
    res = []
    for j in range(i,n+1,kind):
        res.append([j*x+j*(j-1)//2*d, j*x+(j*n-j*(j+1)//2)*d])
    
    if not res: continue
    
    #print(res)
    res.sort()
    
    q = [res[0][:]]

    for e,f in res:
        while q:
            a,b = q[-1]
            if b < e: break
            else:
                q.pop()
                e,f = a,max(b,f)
        
        q.append([e,f])
    
    #print(q)
    for a,b in q:
        ans += (b-a)//d + 1


print(ans)