Buggy python submission (verdict: Runtime Error):
n,q=map(int,input().split())
s=input()
places=[]
moves=[]
for _ in range(q):
  p,m=map(str,input().split())
  places.append(p)
  moves.append(m)
l=-1
r=n
while r-l!=1:
  mid=(l+r)//2
  p=mid
  for i in range(q):
    if s[p]==places[i]:
      if moves[i]=='L':
        p-=1
      if moves[i]=='R':
        p+=1
      if p<0 or p>=n:
        l=mid
        break
  else:
    r=mid
remain_left=r
l=remain_left
r=n
while r-l!=1:
  mid=(l+r)//2
  p=mid
  for i in range(q):
    if s[p]==places[i]:
      if moves[i]=='L':
        p-=1
      if moves[i]=='R':
        p+=1
      if p<0 or p>=n:
        r=mid
        break
  else:
    l=mid
remain_right=l
print(remain_right-remain_left+1)

Fixed submission:
def remain(pos,length):
  p=pos
  for c,d in moves:
    if s[p]==c:
      if d=='L':
        p-=1
      if d=='R':
        p+=1
    if p<0 or p>=length:
      return False
  return True

n,q=map(int,input().split())
s=input()
moves=[list(map(str,input().split())) for _ in range(q)]
l=-1
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    r=mid
  else:
    l=mid
remain_left=r
if remain_left==n:
  print(0)
  exit()
l=remain_left
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    l=mid
  else:
    r=mid
remain_right=l
print(remain_right-remain_left+1)

Buggy python submission (verdict: Time Limit Exceeded):
def remain(pos):
  p=pos
  if p<0 or p>=n:
    return False
  for i in range(q):
    if s[p]==places[i]:
      if moves[i]=='L':
        p-=1
      if moves[i]=='R':
        p+=1
    if p<0 or p>=n:
      return False
    if p>q-i and n-p>q-i:
      return True
  return True

n,q=map(int,input().split())
s=input()
places=[]
moves=[]
for _ in range(q):
  p,m=map(str,input().split())
  places.append(p)
  moves.append(m)
l=-1
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid)==True:
    r=mid
  else:
    l=mid
remain_left=r
l=remain_left
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid)==True:
    l=mid
  else:
    r=mid
remain_right=l
print(remain_right-remain_left+1)

Fixed submission:
def remain(pos,length):
  p=pos
  for c,d in moves:
    if s[p]==c:
      if d=='L':
        p-=1
      if d=='R':
        p+=1
    if p<0 or p>=length:
      return False
  return True

n,q=map(int,input().split())
s=input()
moves=[list(map(str,input().split())) for _ in range(q)]
l=-1
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    r=mid
  else:
    l=mid
remain_left=r
if remain_left==n:
  print(0)
  exit()
l=remain_left
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    l=mid
  else:
    r=mid
remain_right=l
print(remain_right-remain_left+1)

Buggy python submission (verdict: Time Limit Exceeded):
def remain(pos,length):
  p=pos
  if p<0 or p>=length:
    return False
  for c,d in moves:
    if s[p]==c:
      if d=='L':
        p-=1
      if d=='R':
        p+=1
    if p<0 or p>=length:
      return False
  return True

n,q=map(int,input().split())
s=input()
moves=[list(map(str,input().split())) for _ in range(q)]
l=-1
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    r=mid
  else:
    l=mid
remain_left=r
l=remain_left
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    l=mid
  else:
    r=mid
remain_right=l
print(remain_right-remain_left+1)

Fixed submission:
def remain(pos,length):
  p=pos
  for c,d in moves:
    if s[p]==c:
      if d=='L':
        p-=1
      if d=='R':
        p+=1
    if p<0 or p>=length:
      return False
  return True

n,q=map(int,input().split())
s=input()
moves=[list(map(str,input().split())) for _ in range(q)]
l=-1
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    r=mid
  else:
    l=mid
remain_left=r
if remain_left==n:
  print(0)
  exit()
l=remain_left
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    l=mid
  else:
    r=mid
remain_right=l
print(remain_right-remain_left+1)

Buggy python submission (verdict: Time Limit Exceeded):
def remain(pos,length):
  p=pos
  if p<0 or p>=length:
    return False
  for c,d in moves:
    if s[p]==c:
      if d=='L':
        p-=1
      if d=='R':
        p+=1
    if p<0 or p>=length:
      return False
  return True

n,q=map(int,input().split())
s=input()
moves=[list(map(str,input().split())) for _ in range(q)]
l=-1
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    r=mid
  else:
    l=mid
remain_left=r
l=remain_left
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    l=mid
  else:
    r=mid
remain_right=l
print(remain_right-remain_left+1)

Fixed submission:
def remain(pos,length):
  p=pos
  for c,d in moves:
    if s[p]==c:
      if d=='L':
        p-=1
      if d=='R':
        p+=1
    if p<0 or p>=length:
      return False
  return True

n,q=map(int,input().split())
s=input()
moves=[list(map(str,input().split())) for _ in range(q)]
l=-1
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    r=mid
  else:
    l=mid
remain_left=r
if remain_left==n:
  print(0)
  exit()
l=remain_left
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    l=mid
  else:
    r=mid
remain_right=l
print(remain_right-remain_left+1)

Buggy python submission (verdict: Runtime Error):
def remain(pos,length):
  p=pos
  for c,d in moves:
    if s[p]==c:
      if d=='L':
        p-=1
      if d=='R':
        p+=1
    if p<0 or p>=length:
      return False
  return True

n,q=map(int,input().split())
s=input()
moves=[list(map(str,input().split())) for _ in range(q)]
l=-1
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    r=mid
  else:
    l=mid
remain_left=r
l=remain_left
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    l=mid
  else:
    r=mid
remain_right=l
print(remain_right-remain_left+1)

Fixed submission:
def remain(pos,length):
  p=pos
  for c,d in moves:
    if s[p]==c:
      if d=='L':
        p-=1
      if d=='R':
        p+=1
    if p<0 or p>=length:
      return False
  return True

n,q=map(int,input().split())
s=input()
moves=[list(map(str,input().split())) for _ in range(q)]
l=-1
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    r=mid
  else:
    l=mid
remain_left=r
if remain_left==n:
  print(0)
  exit()
l=remain_left
r=n
while r-l!=1:
  mid=(l+r)//2
  if remain(mid,n)==True:
    l=mid
  else:
    r=mid
remain_right=l
print(remain_right-remain_left+1)