Buggy python submission (verdict: Time Limit Exceeded):
mn,jn=map(int,input().split())
mm=input()
mi_mm={}
go=[1 for i in range(mn+2)]
for i in range(mn):
    mi_mm.setdefault(mm[i],[])
    mi_mm[mm[i]]+=[i+1]

for ji in range(jn):
    ju=input().split()
    nk=jn-ji
    if ju[0] in mm[:nk]+mm[-nk:]:
        if ju[1]=="L":
            for mi in mi_mm[ju[0]]:
                if nk+1<=mi<mn-nk-1:continue
                go[mi-1]+=go[mi]
                go[mi]=0
        else:
            for mi in mi_mm[ju[0]][::-1]:
                if nk+1 <= mi < mn - nk-1: continue
                go[mi +1] += go[mi]
                go[mi] = 0

print(sum(go[1:mn+1]))

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, q = MI()
    s = SI()
    td = [SI().split() for _ in range(q)]
    ans = n
    # 左に落ちるゴーレムのうち、最右のインデックス
    l = -1
    r = n
    while l + 1 < r:
        m = (l + r) // 2

        def ok(m):
            for t, d in td:
                if s[m] == t: m += (d == "R") * 2 - 1
                if m == -1: return True
                if m == n: return False
            return False

        if ok(m): l = m
        else: r = m
    ans -= l + 1
    # 右に落ちるゴーレムのうち、最左のインデックス
    l = -1
    r = n
    while l + 1 < r:
        m = (l + r) // 2

        def ok(m):
            for t, d in td:
                if s[m] == t: m += (d == "R") * 2 - 1
                if m == -1: return False
                if m == n: return True
            return False

        if ok(m): r = m
        else: l = m
    ans -= n - r

    print(ans)

main()

Buggy python submission (verdict: Time Limit Exceeded):
mn,jn=map(int,input().split())
mm=input()
mi_mm={}
go=[1 for i in range(mn+2)]
for i in range(mn):
    mi_mm.setdefault(mm[i],[])
    mi_mm[mm[i]]+=[i+1]

for ji in range(jn):
    ju=input().split()
    nk=jn-ji
    if ju[0] in mm[:nk]+mm[-nk:]:
        if ju[1]=="L":
            for mi in mi_mm[ju[0]]:
                go[mi-1]+=go[mi]
                go[mi]=0
        else:
            for mi in mi_mm[ju[0]][::-1]:
                go[mi +1] += go[mi]
                go[mi] = 0

print(sum(go[1:mn+1]))

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, q = MI()
    s = SI()
    td = [SI().split() for _ in range(q)]
    ans = n
    # 左に落ちるゴーレムのうち、最右のインデックス
    l = -1
    r = n
    while l + 1 < r:
        m = (l + r) // 2

        def ok(m):
            for t, d in td:
                if s[m] == t: m += (d == "R") * 2 - 1
                if m == -1: return True
                if m == n: return False
            return False

        if ok(m): l = m
        else: r = m
    ans -= l + 1
    # 右に落ちるゴーレムのうち、最左のインデックス
    l = -1
    r = n
    while l + 1 < r:
        m = (l + r) // 2

        def ok(m):
            for t, d in td:
                if s[m] == t: m += (d == "R") * 2 - 1
                if m == -1: return False
                if m == n: return True
            return False

        if ok(m): r = m
        else: l = m
    ans -= n - r

    print(ans)

main()

Buggy python submission (verdict: Time Limit Exceeded):
mn,jn=map(int,input().split())
mm=input()
mi_mm={}
go=[1 for i in range(mn+2)]
for i in range(mn):
    mi_mm.setdefault(mm[i],[])
    mi_mm[mm[i]]+=[i+1]

for ji in range(jn):
    ju=input().split()
    if ju[0] in mi_mm:
        if ju[1]=="L":
            for mi in mi_mm[ju[0]]:
                go[mi-1]+=go[mi]
                go[mi]=0
        else:
            for mi in mi_mm[ju[0]][::-1]:
                go[mi +1] += go[mi]
                go[mi] = 0

print(sum(go[1:mn+1]))

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n, q = MI()
    s = SI()
    td = [SI().split() for _ in range(q)]
    ans = n
    # 左に落ちるゴーレムのうち、最右のインデックス
    l = -1
    r = n
    while l + 1 < r:
        m = (l + r) // 2

        def ok(m):
            for t, d in td:
                if s[m] == t: m += (d == "R") * 2 - 1
                if m == -1: return True
                if m == n: return False
            return False

        if ok(m): l = m
        else: r = m
    ans -= l + 1
    # 右に落ちるゴーレムのうち、最左のインデックス
    l = -1
    r = n
    while l + 1 < r:
        m = (l + r) // 2

        def ok(m):
            for t, d in td:
                if s[m] == t: m += (d == "R") * 2 - 1
                if m == -1: return False
                if m == n: return True
            return False

        if ok(m): r = m
        else: l = m
    ans -= n - r

    print(ans)

main()