Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
from string import ascii_uppercase
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,Q = read_int()
s = read_str()
td = [list(read_str().split()) for _ in range(Q)]
d = defaultdict(list)
for a in ascii_uppercase:
    d[a] = []
for i in range(N):
    d[s[i]].append(i)
d = dict(d)
lr = {"L":-1, "R":1}

dp = [1 for _ in range(N)]
def solve():
    for i in range(Q):
        char = td[i][0]
        if not d[char]:
            continue

        move = lr[td[i][1]]
        plus = []
        minus = []
        for cur in d[char]:
            if dp[cur] < 1:
                continue
            if cur + move >= 0 and cur + move < N:
                plus.append([cur + move, dp[cur]])
            minus.append([cur, dp[cur]])
        while plus:
            p = plus.pop()
            dp[p[0]] += p[1]
        while minus:
            p = minus.pop()
            dp[p[0]] -= p[1]

if __name__ == "__main__":
    solve()
    print(sum(dp))

Fixed submission:
import sys
from collections import defaultdict
from string import ascii_uppercase
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,Q = read_int()
s = read_str()
td = [list(read_str().split()) for _ in range(Q)]
lr = {"L":-1, "R":1}


def solve():
    # left
    lb = -1
    ub = N
    lefr = (lb + ub) // 2
    while ub - lb > 1:
        lefr = (lb + ub) // 2
        md = lefr
        over = False
        for i in range(Q):
            t, d = td[i]
            if t != s[md]:
                continue
            md += lr[d]
            if md > N - 1:
                over = True
                break
            elif md < 0:
                break
        if over:
            ub = lefr
        else:
            lb = lefr
    l = ub
	
    # right
    lb = -1
    ub = N
    refl = (lb + ub) // 2
    while ub - lb > 1:
        refl = (lb + ub) // 2
        md = refl
        over = False
        for i in range(Q):
            t, d = td[i]
            if t != s[md]:
                continue
            md += lr[d]
            if md < 0:
                over = True
                break
            elif md > N - 1:
                break
        if over:
            lb = refl
        else:
            ub = refl
    r = lb
    res = l - r - 1
    return res

if __name__ == "__main__":
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
from string import ascii_uppercase
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,Q = read_int()
s = read_str()
td = [list(read_str().split()) for _ in range(Q)]
d = defaultdict(list)
for a in ascii_uppercase:
    d[a] = []
for i in range(N):
    d[s[i]].append(i)
d = dict(d)
lr = {"L":-1, "R":1}

dp = [1 for _ in range(N)]
def solve():
    for i in range(Q):
        char = td[i][0]
        move = lr[td[i][1]]
        plus = []
        minus = []
        for cur in d[char]:
            if dp[cur] < 1:
                continue
            if cur + move >= 0 and cur + move < N:
                plus.append([cur + move, dp[cur]])
            minus.append([cur, dp[cur]])
        while plus:
            p = plus.pop()
            dp[p[0]] += p[1]
        while minus:
            p = minus.pop()
            dp[p[0]] -= p[1]


if __name__ == "__main__":
    solve()
    print(sum(dp))

Fixed submission:
import sys
from collections import defaultdict
from string import ascii_uppercase
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,Q = read_int()
s = read_str()
td = [list(read_str().split()) for _ in range(Q)]
lr = {"L":-1, "R":1}


def solve():
    # left
    lb = -1
    ub = N
    lefr = (lb + ub) // 2
    while ub - lb > 1:
        lefr = (lb + ub) // 2
        md = lefr
        over = False
        for i in range(Q):
            t, d = td[i]
            if t != s[md]:
                continue
            md += lr[d]
            if md > N - 1:
                over = True
                break
            elif md < 0:
                break
        if over:
            ub = lefr
        else:
            lb = lefr
    l = ub
	
    # right
    lb = -1
    ub = N
    refl = (lb + ub) // 2
    while ub - lb > 1:
        refl = (lb + ub) // 2
        md = refl
        over = False
        for i in range(Q):
            t, d = td[i]
            if t != s[md]:
                continue
            md += lr[d]
            if md < 0:
                over = True
                break
            elif md > N - 1:
                break
        if over:
            lb = refl
        else:
            ub = refl
    r = lb
    res = l - r - 1
    return res

if __name__ == "__main__":
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
from string import ascii_uppercase
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,Q = read_int()
s = read_str()
td = [list(read_str().split()) for _ in range(Q)]
d = defaultdict(list)
for a in ascii_uppercase:
    d[a] = []
for i in range(N):
    d[s[i]].append(i)
d = dict(d)
lr = {"L":-1, "R":1}

dp = [1 for _ in range(N)]
def solve():
    for i in range(Q):
        char = td[i][0]
        move = lr[td[i][1]]
        plus = []
        minus = []
        for cur in d[char]:
            if dp[cur] < 1:
                continue
            if cur + move >= 0 and cur + move < N:
                for _ in range(dp[cur]):
                    plus.append(cur + move)
            for _ in range(dp[cur]):
                minus.append(cur)
        while plus:
            p = plus.pop()
            dp[p] += 1
        while minus:
            p = minus.pop()
            dp[p] -= 1

if __name__ == "__main__":
    solve()
    print(sum(dp))

Fixed submission:
import sys
from collections import defaultdict
from string import ascii_uppercase
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,Q = read_int()
s = read_str()
td = [list(read_str().split()) for _ in range(Q)]
lr = {"L":-1, "R":1}


def solve():
    # left
    lb = -1
    ub = N
    lefr = (lb + ub) // 2
    while ub - lb > 1:
        lefr = (lb + ub) // 2
        md = lefr
        over = False
        for i in range(Q):
            t, d = td[i]
            if t != s[md]:
                continue
            md += lr[d]
            if md > N - 1:
                over = True
                break
            elif md < 0:
                break
        if over:
            ub = lefr
        else:
            lb = lefr
    l = ub
	
    # right
    lb = -1
    ub = N
    refl = (lb + ub) // 2
    while ub - lb > 1:
        refl = (lb + ub) // 2
        md = refl
        over = False
        for i in range(Q):
            t, d = td[i]
            if t != s[md]:
                continue
            md += lr[d]
            if md < 0:
                over = True
                break
            elif md > N - 1:
                break
        if over:
            lb = refl
        else:
            ub = refl
    r = lb
    res = l - r - 1
    return res

if __name__ == "__main__":
    print(solve())