Problem p03747 (python)

Fixed submission:
import bisect
import sys


def input():
    return sys.stdin.readline()[:-1]

# N = int(input())
# A = [int(x) for x in input().split()]
# a, b, c = map(int, input().split())
# name1 = str(input())
# alph = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
# b = list(map(lambda x: math.ceil(x/10)*10, a))


n, l, t = map(int, input().split())
pos = []
count = 0
for i in range(n):
    x, w = map(int, input().split())
    pos.append((x+t*(3-2*w)) % l)
    count += (x+t*(3-2*w))//l
pos.sort()
count %= n
ans = pos[count:]+pos[:count]
for i in ans:
    print(i)

Buggy submission (verdict: Runtime Error):
import bisect
import sys


def input():
    return sys.stdin.readline()[:-1]

# N = int(input())
# A = [int(x) for x in input().split()]
# a, b, c = map(int, input().split())
# name1 = str(input())
# alph = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
# b = list(map(lambda x: math.ceil(x/10)*10, a))


n, l, t = map(int, input().split())
x = []
w = []
for i in range(n):
    a, b = map(int, input().split())
    x.append(a)
    w.append(b)


def distance(x, y):
    if x <= y:
        return y-x
    else:
        return y+l-x


def forward(x):
    x += 1
    if x == l:
        x = 0
    return x


def back(x):
    x -= 1
    if x == -1:
        x = l-1
    return x


temp_x = x.copy()
temp_w = w.copy()

for t in range(t):
    for i in range(n):
        if i == 0:
            if w[0] == 2:
                if w[n-1] == 1 and distance(x[n-1], x[0]) == 2:
                    temp_x[i] = back(x[i])
                    temp_w[i] = 1
                elif w[n-1] == 1 and distance(x[n-1], x[0]) == 1:
                    temp_w[i] = 1
                else:
                    temp_x[i] = back(x[i])
            elif w[i] == 1 and w[i+1] == 2:
                if distance(x[i], x[i+1]) == 2:
                    temp_x[i] = forward(x[i])
                    temp_w[i] = 2
                elif distance(x[i], x[i+1]) == 1:
                    temp_w[i] = 2
                else:
                    temp_x[i] = forward(x[i])
            elif w[i] == 1:
                temp_x[i] = forward(x[i])
        elif i == n-1:
            if w[n-1] == 1:
                if w[0] == 2 and distance(x[n-1], x[0]) == 2:
                    temp_x[i] = forward(x[i])
                    temp_w[i] = 2
                elif w[0] == 2 and distance(x[n-1], x[0]) == 1:
                    temp_w[i] = 2
                else:
                    temp_x[i] = forward(x[i])
            elif w[i] == 2 and w[i-1] == 1:
                if distance(x[i-1], x[i]) == 2:
                    temp_x[i] = back(x[i])
                    temp_w[i] = 1
                elif distance(x[i-1], x[i]) == 1:
                    temp_w[i] = 1
                else:
                    temp_x[i] = back(x[i])
            elif w[i] == 2:
                temp_x[i] = back(x[i])
        else:
            if w[i] == 1 and w[i+1] == 2:
                if distance(x[i], x[i+1]) == 2:
                    temp_x[i] = forward(x[i])
                    temp_w[i] = 2
                elif distance(x[i], x[i+1]) == 1:
                    temp_w[i] = 2
                else:
                    temp_x[i] = forward(x[i])
            elif w[i] == 1:
                temp_x[i] = forward(x[i])
            elif w[i] == 2 and w[i-1] == 1:
                if distance(x[i-1], x[i]) == 2:
                    temp_x[i] = back(x[i])
                    temp_w[i] = 1
                elif distance(x[i-1], x[i]) == 1:
                    temp_w[i] = 1
                else:
                    temp_x[i] = back(x[i])
            elif w[i] == 2:
                temp_x[i] = back(x[i])
    x = temp_x.copy()
    w = temp_w.copy()
for i in range(n):
    print(x[i])