Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n,q = map(int,input().split())
s = input().rstrip()
td = [list(input().rstrip().split()) for i in range(q)]
def judge(x,direction):
  if direction == "L":
    if x == n:
      return True
    if x == -1:
      return False
    jedge = lambda w: True if w>=0 else False
  else:
    if x == -1:
      return True
    if x == n:
      return False
    jedge = lambda w: True if w<n else False
  for i in range(q):
    if td[i][0] == s[x]:
      if td[i][1] == "R":
        x += 1
      else:
        x -= 1
    if jedge(x) == False:
      return False
  return True
l = -1
r = n
while l+1<r:
  m = (l+r)//2
  if judge(m,"L"):
    r = m
  else:
    l = m
ansl = r
l = -1
r = n
while l+1<r:
  m = (l+r)//2
  if judge(m,"R"):
    l = m
  else:
    r = m
ansr = l
print(max(0,ansr-ansl+1))


Fixed submission:
import sys
input = sys.stdin.readline
n,q = map(int,input().split())
s = input().rstrip()
td = [list(input().rstrip().split()) for i in range(q)]
def judge(x,direction):
  if direction == "L":
    if x == n:
      return True
    if x == -1:
      return False
    jpos = lambda w: True if w>=0 else False
  else:
    if x == -1:
      return True
    if x == n:
      return False
    jpos = lambda w: True if w<n else False
  for i in range(q):
    if td[i][0] == s[x]:
      if td[i][1] == "R":
        x += 1
      else:
        x -= 1
    if jpos(x) == False:
      return False
    if not 0<=x<n:
      return True
  return True
l = -1
r = n
while l+1<r:
  m = (l+r)//2
  if judge(m,"L"):
    r = m
  else:
    l = m
ansl = r
l = -1
r = n
while l+1<r:
  m = (l+r)//2
  if judge(m,"R"):
    l = m
  else:
    r = m
ansr = l
print(max(0,ansr-ansl+1))

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n,q = map(int,input().split())
s = input().rstrip()
td = [list(input().rstrip().split()) for i in range(q)]
def judge(x,direction):
  if direction == "L":
    if x == n:
      return True
    if x == -1:
      return False
    jedge = lambda w: True if w>=0 else False
  else:
    if x == -1:
      return True
    if x == n:
      return False
    jedge = lambda w: True if w<n else False
  for i in range(q):
    if td[i][0] == s[x]:
      if td[i][1] == "R":
        x += 1
      else:
        x -= 1
    if jedge(x) == False:
      return False
  return True
l = -1
r = n
while l+1<r:
  x = (l+r)//2
  if judge(x,"L"):
    r = x
  else:
    l = x
ansl = r
l = -1
r = n
while l+1<r:
  x = (l+r)//2
  if judge(x,"R"):
    l = x
  else:
    r = x
ansr = l
print(max(0,ansr-ansl+1))


Fixed submission:
import sys
input = sys.stdin.readline
n,q = map(int,input().split())
s = input().rstrip()
td = [list(input().rstrip().split()) for i in range(q)]
def judge(x,direction):
  if direction == "L":
    if x == n:
      return True
    if x == -1:
      return False
    jpos = lambda w: True if w>=0 else False
  else:
    if x == -1:
      return True
    if x == n:
      return False
    jpos = lambda w: True if w<n else False
  for i in range(q):
    if td[i][0] == s[x]:
      if td[i][1] == "R":
        x += 1
      else:
        x -= 1
    if jpos(x) == False:
      return False
    if not 0<=x<n:
      return True
  return True
l = -1
r = n
while l+1<r:
  m = (l+r)//2
  if judge(m,"L"):
    r = m
  else:
    l = m
ansl = r
l = -1
r = n
while l+1<r:
  m = (l+r)//2
  if judge(m,"R"):
    l = m
  else:
    r = m
ansr = l
print(max(0,ansr-ansl+1))