Problem: Takahashi has a string S consisting of lowercase English letters.  Starting with this string, he will produce a new one in the procedure given as follows.  The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:    * If T_i = 1: reverse the string S.    * If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.      * If F_i = 1 : Add C_i to the beginning of the string S.     * If F_i = 2 : Add C_i to the end of the string S.  Help Takahashi by finding the final string that results from the procedure.

Submission attempts (in order):
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)


args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def yesno(v):
    print(["Yes", "No"][not v])



def main():
    S, Q = input(str, int)
    front, end, state = "", "", 0
    for _ in range(Q):
        t = int(next(args))
        if t == 1:
            state = (state + 1) % 2
            continue
        F, C = int(next(args)), str(next(args))
        if state == 0:
            if F == 1:
                front = C + front
            else:
                end = end + C
        else:
            if F == 1:
                end = end + C
            else:
                front = C + front
        print(front, end, state)
    ans = front + S + end
    if state == 1:
        ans = end[::-1] + S[::-1] + front[::-1]
    print(ans)


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()


from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)


args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def yesno(v):
    print(["Yes", "No"][not v])



def main():
    S, Q = input(str, int)
    front, end, state = "", "", 0
    for _ in range(Q):
        t = int(next(args))
        if t == 1:
            state = (state + 1) % 2
            continue
        F, C = int(next(args)), str(next(args))
        if state == 0:
            if F == 1:
                front = C + front
            else:
                end = end + C
        else:
            if F == 1:
                end = end + C
            else:
                front = C + front
    ans = front + S + end
    if state == 1:
        ans = end[::-1] + S[::-1] + front[::-1]
    print(ans)


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()


Verdicts: Runtime Error

Accepted