Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import collections

if __name__ == '__main__':
    N, W = map(int, input().split())

    dp = [collections.defaultdict(int) for _ in range(N + 1)]
    dp[0][0] = 0

    for i in range(N):
        w, v = map(int, input().split())
        for j in range(W + 1):
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + w <= W: dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)

    ans = max(dp[N].values())
    print(ans)


Fixed submission:
import collections

if __name__ == '__main__':
    N, W = map(int, input().split())

    dp = [collections.defaultdict(int) for _ in range(N + 1)]
    dp[0][0] = 0

    for i in range(N):
        w, v = map(int, input().split())
        for j in dp[i]:
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + w <= W: dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)

    ans = max(dp[N].values())
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == '__main__':
    N, W = map(int, input().split())

    dp = [[-1 for _ in range(W + 1)] for _ in range(N + 1)]
    dp[0][0] = 0

    for i in range(N):
        w, v = map(int, input().split())
        for j in range(W + 1):
            if dp[i][j] == -1: continue
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + w <= W: dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)

    ans = max(dp[N])
    print(ans)


Fixed submission:
import collections

if __name__ == '__main__':
    N, W = map(int, input().split())

    dp = [collections.defaultdict(int) for _ in range(N + 1)]
    dp[0][0] = 0

    for i in range(N):
        w, v = map(int, input().split())
        for j in dp[i]:
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + w <= W: dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)

    ans = max(dp[N].values())
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == '__main__':
    N, W = map(int, input().split())

    dp = [[-1 for _ in range(W + 1)] for _ in range(N + 1)]
    dp[0][0] = 0

    for i in range(N):
        w, v = map(int, input().split())
        for j in range(W + 1):
            if dp[i][j] == -1: continue
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + w <= W: dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)

    ans = max(dp[N])
    print(ans)


Fixed submission:
import collections

if __name__ == '__main__':
    N, W = map(int, input().split())

    dp = [collections.defaultdict(int) for _ in range(N + 1)]
    dp[0][0] = 0

    for i in range(N):
        w, v = map(int, input().split())
        for j in dp[i]:
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + w <= W: dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)

    ans = max(dp[N].values())
    print(ans)
Buggy python submission (verdict: Runtime Error):
sx, sy, tx, ty = map(int, input().split())
dx, dy = tx - sx, ty - sy

ans = "" 
ans += 'U' * dy + 'R' * dx 
ans += 'D' * dy + 'L' * dx
ans += 'L'
ans += 'U' * (dy + 1) + 'R' * (dx + 1)
ans += 'D'
ans += 'R'
ans += 'D' * (dy + 1) + 'L' * (dx + 1)
ans += 'U'

print(ans)

Fixed submission:
s = input()
t = s.replace(',', ' ')
print(t)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools

n, m = map(int, input().split())
d = []
for _ in range(m):
    a, b, c = map(int, input().split())
    d.append((a - 1, b - 1, c))

wf = [[float('inf')] * n for _ in range(n)]
for a, b, c in d:
    wf[a][b] = wf[b][a] = c
for i in range(n):
    wf[i][i] = 0

for k, i, j in itertools.product(range(n), repeat=3):
    wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j])

ans = 0
for a, b, c in d:
    used = False
    for i, j in itertools.product(range(n), repeat=2):
        if wf[i][a] + c + wf[b][j] == wf[i][j] or wf[i][b] + c + wf[a][j] == wf[i][j]:
            used = True
    
    if not used:
        ans += 1

print(ans)

Fixed submission:
import itertools

n, m = map(int, input().split())
d = []
for _ in range(m):
    a, b, c = map(int, input().split())
    d.append((a - 1, b - 1, c))

wf = [[float('inf')] * n for _ in range(n)]
for a, b, c in d:
    wf[a][b] = wf[b][a] = c
for i in range(n):
    wf[i][i] = 0

for k, i, j in itertools.product(range(n), repeat=3):
    wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j])

ans = 0
for a, b, c in d:
    used = False
    for i in range(n):
        for j in range(i + 1, n):
            if wf[i][a] + c + wf[b][j] == wf[i][j] \
                    or wf[i][b] + c + wf[a][j] == wf[i][j] \
                    or wf[j][a] + c + wf[b][i] == wf[j][i] \
                    or wf[j][b] + c + wf[a][i] == wf[j][i]:
                used = True
            
    if not used:
        ans += 1

print(ans)

Buggy python submission (verdict: Runtime Error):
import itertools

n, m = map(int, input().split())
a = [None] * n
b = [None] * n
c = [None] * n


for i in range(m):
    a[i], b[i], c[i] = map(int, input().split())
    a[i] -= 1
    b[i] -= 1

wf = [[float('inf')] * n for _ in range(n)]
for i in range(n):
    wf[i][i] = 0
for i in range(m):
    wf[a[i]][b[i]] = wf[b[i]][a[i]] = c[i]

for k, i, j in itertools.product(range(n), repeat=3):
    wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j])

ans = 0
for x in range(m):
    used = False
    for i, j in itertools.product(range(n), repeat=2):
        if wf[i][a[x]] + c[x] + wf[b[x]][j] == wf[i][j] or wf[i][b[x]] + c[x] + wf[a[x]][j] == wf[i][j]:
            used = True
    
    if not used:
        ans += 1

print(ans)


Fixed submission:
import itertools

n, m = map(int, input().split())
d = []
for _ in range(m):
    a, b, c = map(int, input().split())
    d.append((a - 1, b - 1, c))

wf = [[float('inf')] * n for _ in range(n)]
for a, b, c in d:
    wf[a][b] = wf[b][a] = c
for i in range(n):
    wf[i][i] = 0

for k, i, j in itertools.product(range(n), repeat=3):
    wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j])

ans = 0
for a, b, c in d:
    used = False
    for i in range(n):
        for j in range(i + 1, n):
            if wf[i][a] + c + wf[b][j] == wf[i][j] \
                    or wf[i][b] + c + wf[a][j] == wf[i][j] \
                    or wf[j][a] + c + wf[b][i] == wf[j][i] \
                    or wf[j][b] + c + wf[a][i] == wf[j][i]:
                used = True
            
    if not used:
        ans += 1

print(ans)
Buggy python submission (verdict: Runtime Error):
def solve(s):
    if len(s) == 0:
        return True
    return any(solve(s[len(prefix):]) for prefix in ('dreamer', 'dream', 'eraser', 'erase') if s.startswith(prefix))

print('YES' if solve(input()) else 'NO')

Fixed submission:
def solve(s):
    stack = [s]
    while stack:
        word = stack.pop()
        if len(word) == 0:
            return True
        for prefix in ('dreamer', 'dream', 'eraser', 'erase'):
            if word.startswith(prefix):
                stack.append(word[len(prefix):])

print('YES' if solve(input()) else 'NO')
Buggy python submission (verdict: Time Limit Exceeded):
def find(parents, x):
    if parents[x] == x:
        return x
    else:
        parents[x] = find(parents, parents[x])
        return parents[x]

def union(parents, x1, x2):
    y1 = find(parents, x1)
    y2 = find(parents, x2)
    if y1 != y2:
        parents[y1] = y2


n, k, l = map(int, input().split())

parents1 = list(range(n))
for _ in range(k):
    p, q = map(int, input().split())
    union(parents1, p - 1, q - 1)

parents2 = list(range(n))
for _ in range(l):
    r, s = map(int, input().split())
    union(parents2, r - 1, s - 1)

counter = [1] * n
for i in range(n):
    for j in range(i + 1, n):
        if find(parents1, i) == find(parents1, j) and find(parents2, i) == find(parents2, j):
            counter[i] += 1
            counter[j] += 1
print(' '.join(map(str, counter)))


Fixed submission:
def find(parents, x):
    if parents[x] == x:
        return x
    else:
        parents[x] = find(parents, parents[x])
        return parents[x]

def union(parents, x1, x2):
    y1 = find(parents, x1)
    y2 = find(parents, x2)
    if y1 != y2:
        parents[y1] = y2


n, k, l = map(int, input().split())

parents1 = list(range(n))
for _ in range(k):
    p, q = map(int, input().split())
    union(parents1, p - 1, q - 1)

parents2 = list(range(n))
for _ in range(l):
    r, s = map(int, input().split())
    union(parents2, r - 1, s - 1)

import collections

counter = collections.defaultdict(int)
for i in range(n):
    p1, p2 = find(parents1, i), find(parents2, i)
    counter[(p1, p2)] += 1

ans = ' '.join(str(counter[(find(parents1, i), find(parents2, i))]) for i in range(n))
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
p = [int(s) - 1 for s in input().split()]

a = [40_000 * (i + 1) for i in range(n)]
b = list(reversed(a))
for i in range(n):
    a[p[i]] += i

print(' '.join(map(str, a)))
print(' '.join(map(str, b)))

Fixed submission:
n = int(input())
p = [int(s) - 1 for s in input().split()]

a = [40000 * (i + 1) for i in range(n)]
b = list(reversed(a))
for i in range(n):
    a[p[i]] += i

print(' '.join(map(str, a)))
print(' '.join(map(str, b)))
Buggy python submission (verdict: Runtime Error):
s = input()
x = s.index('C')
y = s.rindex('F')
if x >= 0 and y >= 0 and x < y:
    print('Yes')
else:
    print('No') 

Fixed submission:
s = input()
x = s.find('C')
y = s.rfind('F')
if x >= 0 and y >= 0 and x < y:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
from collections import defaultdict
readline = stdin.readline
def i_input(): return int(readline().rstrip())
def i_map(): return map(int, readline().rstrip().split())
def i_list(): return list(i_map())


def main():
    N = i_input()
    d = defaultdict(lambda: 0)
    ans = 0
    for i in range(1, N):
        for j in range(i, N, i):
            d[j] += 1
    for c in range(1, N):
        ab = N - c
        ans += d[ab]
    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
from sys import stdin
from collections import defaultdict
readline = stdin.readline
def i_input(): return int(readline().rstrip())
def i_map(): return map(int, readline().rstrip().split())
def i_list(): return list(i_map())


def main():
    N = i_input()
    d = defaultdict(lambda: 1)
    ans = 0
    for i in range(2, N):
        for j in range(i, N, i):
            d[j] += 1
    for c in range(1, N):
        ans += d[c]
    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
readline = stdin.readline
def i_input(): return int(readline().rstrip())
def i_map(): return map(int, readline().rstrip().split())
def i_list(): return list(i_map())

def make_divisors(n):
    result = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            result.append(i)
            if i != n // i:
                result.append(n//i)
    result.sort()
    return result

def main():
    N = i_input()
    ans = 0
    for c in range(1, N):
        ab = make_divisors(N - c)
        ans += len(ab)
    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
from sys import stdin
from collections import defaultdict
readline = stdin.readline
def i_input(): return int(readline().rstrip())
def i_map(): return map(int, readline().rstrip().split())
def i_list(): return list(i_map())


def main():
    N = i_input()
    d = defaultdict(lambda: 1)
    ans = 0
    for i in range(2, N):
        for j in range(i, N, i):
            d[j] += 1
    for c in range(1, N):
        ans += d[c]
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
from collections import defaultdict
readline = stdin.readline
read = stdin.buffer.read
def r_map(): return map(int, readline().rstrip().split())
def r_list(): return list(r_map())

MOD = 10 ** 9 + 7
# import numpy as np
# from numba import jit
# stdin = open('sample.txt')

def main():
    N = int(readline().rstrip())
    A = r_list()
    A.sort()
    ans = 0
    a_dic = defaultdict(lambda: 0)
    for i, a in enumerate(A[:-1]):
        for aa in A[i + 1:]:
            a_dic[a] += aa
    for a in A[:-1]:
        ans += a * a_dic[a]
    print(ans % MOD)

if __name__ == "__main__":
    main()


Fixed submission:
from sys import stdin
from collections import defaultdict
readline = stdin.readline
read = stdin.buffer.read
def r_map(): return map(int, readline().rstrip().split())
def r_list(): return list(r_map())

MOD = 10 ** 9 + 7
# import numpy as np
# from numba import jit
# stdin = open('sample.txt')

def main():
    N = int(readline().rstrip())
    A = r_list()
    A.sort()
    ans = 0
    a_dic = defaultdict(lambda: 0)
    for j, a in enumerate(A[:0:-1]):
        i = N - 2 - j
        a_dic[i] += a
        if i > 0:
            a_dic[i - 1] = a_dic[i]
    for i, a in enumerate(A[:-1]):
        ans += a * a_dic[i]
    print(ans % MOD)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
from sys import stdin
from collections import deque, defaultdict

readline = stdin.readline

def main():
    H, W = map(int, readline().rstrip().split())
    CH, CW = map(lambda x: int(x - 1), readline().rstrip().split())
    DH, DW = map(lambda x: int(x - 1), readline().rstrip().split())
    S = [readline().rstrip() for _ in range(H)]
    v = defaultdict(lambda : False)
    dq = deque([(0, CH, CW)])
    while dq:
        d, h, w = dq.popleft()
        # print(f'{c} : {h} : {w} : {S[h][w]}')
        if (h, w) == (DH, DW):
            print(d)
            exit()
        if v[(h, w)]:
            continue
        v[(h, w)] = True
        for mh, mw in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            nh = mh + h
            nw = mw + wa
            if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == '.' and not v[(nh, nw)]:
                dq.appendleft([d, nh, nw])
        for mh in range(-2, 3):
            for mw in range(-2, 3):
                if h == w == 0:
                    continue
                nh = mh + h
                nw = mw + w
                if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == '.' and not v[(nh, nw)]:
                    dq.append([d + 1, nh, nw])
    print(-1)

if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
Ch, Cw = map(lambda x: int(x) - 1, input().split())
Dh, Dw = map(lambda x: int(x) - 1, input().split())
S = [input().rstrip() for _ in range(H)]
visited = [[False] * W for _ in range(H)]
dq = deque([(0, Ch, Cw)])
while dq:
    d, i, j = dq.popleft()
    if (i, j) == (Dh, Dw):
        print(d)
        exit()
    if visited[i][j]:
        continue
    visited[i][j] = True
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and S[ni][nj] == '.':
            dq.appendleft((d, ni, nj))
    for di in range(-2, 3):
        for dj in range(-2, 3):
            if di == dj == 0:
                continue
            ni, nj = i + di, j + dj
            if 0 <= ni < H and 0 <= nj < W and S[ni][nj] == '.' and not visited[ni][nj]:
                dq.append((d + 1, ni, nj))
print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
c = input()
ans = 0
for _ in range(N):
    fs = 0
    ls = len(c)
    for i, ci in enumerate(c):
        fs = i
        if ci == 'W':
            break
    for i , ci in enumerate(c[::-1]):
        if i + fs >= N:
            break
        ls -= 1
        if ci == 'R':
            break
    if fs == ls:
        break
    ans += 1
    c = c[fs + 1:ls]
    #print(c)
    if len(c) <= 1:
        break
print(ans)

Fixed submission:
N = int(input())
C = input()
a = b = ans = 0
for c in C:
    if c == 'R':
        a += 1
ans = max(a, b)
for c in C:
    if c == 'R':
        a -= 1
    else:
        b += 1
    ans = min(ans, max(a, b))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
c = input()
ans = 0
for _ in range(N):
    fs = 0
    ls = len(c)
    for i, ci in enumerate(c):
        fs = i
        if ci == 'W':
            break
    for i , ci in enumerate(c[::-1]):
        if i + fs >= N:
            break
        ls -= 1
        if ci == 'R':
            break
    if fs == ls:
        break
    ans += 1
    c = c[fs + 1:ls]
    #print(c)
    if len(c) <= 1:
        break
print(ans)



Fixed submission:
N = int(input())
C = input()
a = b = ans = 0
for c in C:
    if c == 'R':
        a += 1
ans = max(a, b)
for c in C:
    if c == 'R':
        a -= 1
    else:
        b += 1
    ans = min(ans, max(a, b))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))
bs = 1
for i in range(K):
    bs *= A[i]
for i in range(N - K):
    s = bs * A[K + i] // A[i]
    if s > bs:
        print('Yes')
    else:
        print('No')
    bs = s


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N - K):
    if A[K + i] >  A[i]:
        print('Yes')
    else:
        print('No')
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
ans = 1000
i = 0
while(len(A) - i > 1):
    s = [s for s in A[i + 1:] if s > A[i]]
    if len(s) == 0:
        break
    si = A.index(s[0])
    b = min(A[i:si])
    stock = ans // b
    ans %= b
    ans += (stock * s[0])
    i = si + 1
    
print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
ans = 1000
for i in range(len(A) - 1):
    stocks = 0
    if A[i] < A[i + 1]:
        stocks = ans // A[i]
        ans += (A[i + 1] - A[i]) * stocks
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def popcont(n):
    return bin(n).count('1')

def f(n):
    nn = n
    fcnt = 0
    while(nn > 0):
        nn = nn % popcont(nn)
        fcnt += 1
    return fcnt

N = int(input())
X = int(input(), 2)
for i in range(N - 1, -1, -1):
    rbx = (X & ~pow(2, i))
    rby = ~X & pow(2, i)
    rbz = bin(rbx | rby)
    print(f(int(rbz, 0)))

Fixed submission:
# https://atcoder.jp/contests/aising2020/tasks/aising2020_d

def goes_to_zero(n):
    cnt = 0
    while(n > 0):
        n %= bin(n).count('1')
        cnt += 1
    return cnt

N = int(input())
X = input()
XC = X.count('1')
XI = int(X, 2)
XCP = XC + 1
XCM = XC - 1
XTP = XI % XCP
XTM = XI % XCM if XCM != 0 else 0
for i in range(N):
    if X[i] == "0":
        print(goes_to_zero((XTP + pow(2, N - 1 - i, XCP)) % XCP) + 1)
    else:
        if XCM == 0:
            print(0)
        else:
            print(goes_to_zero((XTM - pow(2,N - 1 - i, XCM)) % XCM) + 1)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, M, K  = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    bs = M
    ans = 0
    for i in range(N + 1):
        asum = sum(A[:i])
        if asum > K:
            break
        for j in range(bs, -1, -1):
            if sum(B[:j]) + asum <= K:
                bs = j
                break
        ans = max(ans, i + bs)
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    N, M, K  = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    a, b = [0], [0]
    for i in range(N):
        a.append(a[i] + A[i])
    for i in range(M):
        b.append(b[i] + B[i])
    bs = M
    ans = 0
    for i in range(N + 1):
        if a[i] > K:
            break
        for j in range(bs, -1, -1):
            if b[j] + a[i] <= K:
                bs = j
                break
        ans = max(ans, i + bs)
    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    n, m, k  = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]
    aa = 0
    bs = 0
    cc = 0
    for i in range(n + 1):
        c = 0
        if sum(a[:i]) > k:
            break
        else:
            aa = sum(a[:i])
            c = i
            for j in range(bs, m + 1):
                if sum(b[:-j if j != 0 else m]) + aa <= k:
                    c += len(b) - j
                    bs = j
                    break
                else:
                    continue
            if c > cc:
                cc = c
    print(cc)

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    N, M, K  = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    a, b = [0], [0]
    for i in range(N):
        a.append(a[i] + A[i])
    for i in range(M):
        b.append(b[i] + B[i])
    bs = M
    ans = 0
    for i in range(N + 1):
        if a[i] > K:
            break
        for j in range(bs, -1, -1):
            if b[j] + a[i] <= K:
                bs = j
                break
        ans = max(ans, i + bs)
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
A = map(int, input().split())
c = Counter(A)
Q = int(input())
for _ in range(Q):
    B, Q = map(int, input().split())
    c[Q] += c[B]
    c[B] = 0
    summ = 0
    for k, v in c.items():
        summ += k * v
    print(summ)


Fixed submission:
from collections import Counter
N = int(input())
A = map(int, input().split())
c = Counter(A)
summ = 0
for k, v in c.items():
    summ += k * v
Q = int(input())
for _ in range(Q):
    B, Q = map(int, input().split())
    summ += Q * c[B] - B * c[B]
    c[Q] += c[B]
    c[B] = 0
    print(summ)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
A = map(int, input().split())
c = Counter(A)
Q = int(input())
for _ in range(Q):
    B, Q = map(int, input().split())
    c[Q] += c[B]
    c[B] = 0
    summ = 0
    for k, v in c.items():
        summ += k * v
    print(summ)


Fixed submission:
from collections import Counter
N = int(input())
A = map(int, input().split())
c = Counter(A)
summ = 0
for k, v in c.items():
    summ += k * v
Q = int(input())
for _ in range(Q):
    B, Q = map(int, input().split())
    summ += Q * c[B] - B * c[B]
    c[Q] += c[B]
    c[B] = 0
    print(summ)
Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
# stdin = open('b_1.txt')

input = stdin.readline

MOD = 998244353
def main():
    A, B, C, D = map(int, input().rstrip().split())
    DP = [[0 for _ in range(D + 1)] for _ in range(C + 1)]
    for i in range(A, C + 1):
        for j in range(B, D + 1):
            if i == A and j == B:
                DP[i][j] = 1
                continue
            DP[i][j] = ((DP[i -1][j] * j) + (DP[i][j -1] * i) - (DP[i -1][j - 1] * (i - 1) * (j  - 1))) % MOD
    print(DP[C][D] % MOD)

if __name__ == "__main__":
    main()



Fixed submission:
from sys import stdin
# stdin = open('b_1.txt')

input = stdin.readline

MOD = 998244353
def main():
    A, B, C, D = map(int, input().rstrip().split())
    DP = [[0 for _ in range(D + 1)] for _ in range(C + 1)]
    for i in range(A, C + 1):
        for j in range(B, D + 1):
            if i == A and j == B:
                DP[i][j] = 1
                continue
            DP[i][j] = ((DP[i -1][j] * j) + (DP[i][j -1] * i) - (DP[i -1][j - 1] * (i - 1) * (j  - 1))) % MOD
    print(DP[C][D] % MOD)

if __name__ == "__main__":
    main()