Buggy python submission (verdict: Time Limit Exceeded):

def main():
    import sys
    def input():
        return sys.stdin.readline()[:-1]

    N = int(input())
    S = input()

    if S[0]!='B' or S[-1]!='B':
        print(0)
        exit()

    mod = 10**9 + 7
    cnt = 1
    pre = 'B'
    d = 1 #1がleft
    ans = 1

    for i in range(1,N*2):
        if S[i-1]==S[i]:
            if d:
                d = 0
                ans *= cnt%mod
                cnt -= 1
            else:
                d = 1
                cnt += 1
        else:
            if d:
                cnt += 1
            else:
                ans *= cnt%mod
                cnt -= 1

    for i in range(1,N+1):
        ans *= i
        ans %= mod
    if cnt!=0:
        print(0)
        exit()

    print(ans)

if __name__=='__main__':
    main()

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):

def main():
    import sys
    def input():
        return sys.stdin.readline()[:-1]

    N = int(input())
    S = list(input())

    if S[0]!='B' or S[-1]!='B':
        print(0)
        exit()

    mod = 10**9 + 7
    cnt = 1
    pre = 'B'
    d = 1 #1がleft
    ans = 1

    for i in range(1,N*2):
        if S[i-1]==S[i]:
            if d:
                d = 0
                ans *= cnt%mod
                cnt -= 1
            else:
                d = 1
                cnt += 1
        else:
            if d:
                cnt += 1
            else:
                ans *= cnt%mod
                cnt -= 1

    for i in range(1,N+1):
        ans *= i
        ans %= mod
    if cnt!=0:
        print(0)
        exit()

    print(ans)

if __name__=='__main__':
    main()

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    def input():
        return sys.stdin.readline()[:-1]

    N = int(input())
    S = list(input())

    if S[0]!='B' or S[-1]!='B':
        print(0)
        exit()

    mod = 10**9 + 7
    cnt_l = 1
    cnt = 1
    pre = 'B'
    d = 1 #1がleft
    ans = 1

    for i in range(1,N*2):
        if S[i-1]==S[i]:
            if d:
                d = 0
                ans *= cnt%mod
                cnt -= 1
            else:
                d = 1
                cnt_l += 1
                cnt += 1
        else:
            if d:
                cnt_l += 1
                cnt += 1
            else:
                ans *= cnt%mod
                cnt -= 1

    for i in range(1,N+1):
        ans *= i
        ans %= mod
    if cnt_l!=N:
        print(0)
        exit()

    print(ans)

if __name__=='__main__':
    main()

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Runtime Error):
import math
import sys
from copy import copy
import numpy as np
from collections import defaultdict, deque, Counter
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)

n = int(input())
s = list(input())

count = 0


if s[0] == 'W' or s[-1] == 'W':
    print(0)
    sys.exit()

LR_list = ['' for i in range(2*n)]
LR_list[0] = 'L'

for i in range(1,len(LR_list)):
    if s[i] == s[i-1]:
        if LR_list[i-1] == 'R':
            LR_list[i] = 'L'
        else:
            LR_list[i] = 'R'
    else:
        if LR_list[i-1] == 'R':
            LR_list[i] = 'R'
        else:
            LR_list[i] = 'L'

values = []

if LR_list.count('L') != LR_list.count('R'):
    print(0)
    sys.exit()


li = 0
ri = 0
for i in range(len(LR_list)):
    if i != 0:
        if LR_list[i-1] == 'L':
            li += 1
        elif LR_list[i-1] == 'R':
            ri += 1
    if LR_list[i] == 'R':
        values.append(li-ri)
value = 1
for i in range(len(values)):
    value = value * values[i] % (10**9+7)
for i in range(2,n+1):
    value = value * i %(10**9+7)
print(value)

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Runtime Error):
def main():
    import math
    import sys
    from copy import copy
    import numpy as np
    from collections import defaultdict, deque, Counter
    from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
    from operator import itemgetter
    from bisect import bisect_left,bisect
    from heapq import heappop,heappush
    from fractions import gcd

    max_value = 1000000000
    min_value = -1000000000
    sys.setrecursionlimit(max_value)

    def input():
        return sys.stdin.readline()[:-1]

    n = int(input())
    s = list(input())

    count = 0


    if s[0] == 'W' or s[-1] == 'W':
        print(0)
        sys.exit()

    LR_list = ['' for i in range(2*n)]
    LR_list[0] = 'L'

    for i in range(1,len(LR_list)):
        if s[i] == s[i-1]:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'L'
            else:
                LR_list[i] = 'R'
        else:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'R'
            else:
                LR_list[i] = 'L'

    values = []

    if LR_list.count('L') != LR_list.count('R'):
        print(0)
        sys.exit()


    li = 0
    ri = 0
    for i in range(len(LR_list)):
        if i != 0:
            if LR_list[i-1] == 'L':
                li += 1
            elif LR_list[i-1] == 'R':
                ri += 1
        if LR_list[i] == 'R':
            values.append(li-ri)
    value = 1
    for i in range(len(values)):
        value = value * values[i] % (10**9+7)
    for i in range(2,n+1):
        value = value * i %(10**9+7)
    print(value)


if __name__=='__main__':
    main()

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Runtime Error):
def main():
    import math
    import sys
    from copy import copy
    import numpy as np
    from collections import defaultdict, deque, Counter
    from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
    from operator import itemgetter
    from bisect import bisect_left,bisect
    from heapq import heappop,heappush
    from fractions import gcd

    max_value = 1000000000
    min_value = -1000000000
    sys.setrecursionlimit(max_value)

    n = int(input())
    s = list(input())

    count = 0


    if s[0] == 'W' or s[-1] == 'W':
        print(0)
        sys.exit()

    LR_list = ['' for i in range(2*n)]
    LR_list[0] = 'L'

    for i in range(1,len(LR_list)):
        if s[i] == s[i-1]:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'L'
            else:
                LR_list[i] = 'R'
        else:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'R'
            else:
                LR_list[i] = 'L'

    values = []

    if LR_list.count('L') != LR_list.count('R'):
        print(0)
        sys.exit()


    li = 0
    ri = 0
    for i in range(len(LR_list)):
        if i != 0:
            if LR_list[i-1] == 'L':
                li += 1
            elif LR_list[i-1] == 'R':
                ri += 1
        if LR_list[i] == 'R':
            values.append(li-ri)
    value = 1
    for i in range(len(values)):
        value = value * values[i] % (10**9+7)
    for i in range(2,n+1):
        value = value * i %(10**9+7)
    print(value)



if __name__=='__main__':
    main()

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Runtime Error):
def main():
    n = int(input())
    s = list(input())

    count = 0


    if s[0] == 'W' or s[-1] == 'W':
        print(0)
        sys.exit()

    LR_list = ['' for i in range(2*n)]
    LR_list[0] = 'L'

    for i in range(1,len(LR_list)):
        if s[i] == s[i-1]:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'L'
            else:
                LR_list[i] = 'R'
        else:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'R'
            else:
                LR_list[i] = 'L'

    values = []

    if LR_list.count('L') != LR_list.count('R'):
        print(0)
        sys.exit()


    li = 0
    ri = 0
    for i in range(len(LR_list)):
        if i != 0:
            if LR_list[i-1] == 'L':
                li += 1
            elif LR_list[i-1] == 'R':
                ri += 1
        if LR_list[i] == 'R':
            values.append(li-ri)
    value = 1
    for i in range(len(values)):
        value = value * values[i] % (10**9+7)
    for i in range(2,n+1):
        value = value * i %(10**9+7)
    print(value)



if __name__=='__main__':
    main()

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    def input():
        return sys.stdin.readline()[:-1]

    N = int(input())
    S = list(input())

    if S[0]!='B' or S[-1]!='B':
        print(0)
        exit()

    mod = 10**9 + 7
    cnt_l = 1
    cnt_r = 0
    pre = 'B'
    pre_d = 1 #1がleft
    ans = 1

    for i in range(1,N*2):
        if S[i-1]==S[i]:
            if pre_d:
                pre_d = 0
                ans *= (cnt_l - cnt_r)%mod
                cnt_r += 1
            else:
                pre_d = 1
                cnt_l += 1
        else:
            if pre_d:
                cnt_l += 1
            else:
                ans *= (cnt_l - cnt_r)%mod
                cnt_r += 1

    for i in range(1,N+1):
        ans *= i
        ans %= mod
    if cnt_l!=N:
        print(0)
        exit()

    print(ans)

if __name__=='__main__':
    main()

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    def input():
        return sys.stdin.readline()[:-1]

    N = int(input())
    S = list(input())

    if S[0]!='B' or S[-1]!='B':
        print(0)
        exit()

    mod = 10**9 + 7
    cnt_l = 1
    cnt_r = 0
    pre = 'B'
    pre_d = 1 #1がleft
    ans = 1

    for i in range(1,N*2):
        if S[i-1]==S[i]:
            if pre_d:
                pre_d = 0
                ans *= cnt_l - cnt_r
                cnt_r += 1
            else:
                pre_d = 1
                cnt_l += 1
        else:
            if pre_d:
                cnt_l += 1
            else:
                ans *= cnt_l - cnt_r
                cnt_r += 1

    for i in range(1,N+1):
        ans *= i
        ans %= mod
    if cnt_l!=N:
        print(0)
        exit()

    print(ans)

if __name__=='__main__':
    main()


Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    def input():
        return sys.stdin.readline()[:-1]

    N = int(input())
    S = list(input())

    if S[0]!='B' or S[-1]!='B':
        print(0)
        exit()

    mod = 10**9 + 7
    cnt_l = 1
    cnt_r = 0
    pre = 'B'
    pre_d = 1 #1がleft
    ans = 1

    for i in range(1,N*2):
        s = S[i]
        pre = S[i-1]
        if pre==s:
            if pre_d:
                pre_d = 0
                ans *= cnt_l - cnt_r #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
                cnt_r += 1
            else:
                pre_d = 1
                cnt_l += 1
        else:
            if pre_d:
                cnt_l += 1
            else:
                ans *= cnt_l - cnt_r #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
                cnt_r += 1

    for i in range(1,N+1):
        ans *= i
        ans %= mod
    if cnt_l!=N:
        print(0)
        exit()

    print(ans)

if __name__=='__main__':
    main()

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
S = list(input())

if S[0]!='B' or S[-1]!='B':
    print(0)
    exit()

mod = 10**9 + 7
cnt_l = 1
cnt_r = 0
pre_s = 'B'
pre_d = 1 #1がleft
ans = 1

for i,s in enumerate(S[1:]):
    if pre_s==s:
        if pre_d:
            pre_d = 0
            ans *= cnt_l - cnt_r #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
            cnt_r += 1
        else:
            pre_d = 1
            cnt_l += 1
    else:
        if pre_d:
            cnt_l += 1
        else:
            ans *= cnt_l - cnt_r #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
            cnt_r += 1
    pre_s = s

for i in range(1,N+1):
    ans *= i
    ans %= mod
if cnt_l!=N:
    print(0)
    exit()

print(ans)

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
S = list(input())

if S[0]!='B' or S[-1]!='B':
    print(0)
    exit()

mod = 10**9 + 7
cnt_l = 1
cnt_r = 0
pre_s = 'B'
pre_d = 1 #1がleft
ans = 1

for i in range(1,N*2):
    s = S[i]

    if pre_s==s:
        if pre_d:
            pre_d = 0
            ans *= cnt_l - cnt_r #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
            cnt_r += 1
        else:
            pre_d = 1
            cnt_l += 1
    else:
        if pre_d:
            cnt_l += 1
        else:
            ans *= cnt_l - cnt_r #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
            cnt_r += 1
    pre_s = s

for i in range(1,N+1):
    ans *= i
    ans %= mod
if cnt_l!=N:
    print(0)
    exit()

print(ans)

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
S = list(input())

if S[0]!='B' or S[-1]!='B':
    print(0)
    exit()

mod = 10**9 + 7
cnt_l = 1
cnt_r = 0
pre_s = 'B'
pre_d = 1 #1がleft
ans = 1

for i in range(1,N*2):
    s = S[i]

    if pre_s==s:
        if pre_d==1:
            d = 0
            ans *= cnt_l - cnt_r #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
            cnt_r += 1
        else:
            d = 1
            cnt_l += 1
    else:
        if pre_d==1:
            d = 1
            cnt_l += 1
        else:
            d = 0
            ans *= cnt_l - cnt_r #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
            cnt_r += 1
    pre_s = s
    pre_d = d

for i in range(1,N+1):
    ans *= i
    ans %= mod
if cnt_l!=N:
    print(0)
    exit()

print(ans)

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
S = list(input())

if S[0]!='B' or S[-1]!='B':
    print(0)
    exit()

mod = 10**9 + 7
cnt_l = 1
cnt_r = 0
pre_s = 'B'
pre_d = 1 #1がleft
ans = 1

for i in range(1,N*2):
    s = S[i]

    if pre_s==s:
        if pre_d==1:
            d = 0
            ans *= cnt_l - cnt_r #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
            cnt_r += 1
        else:
            d = 1
            cnt_l += 1
    else:
        if pre_d==1:
            d = 1
            cnt_l += 1
        else:
            d = 0
            ans *= cnt_l - cnt_r #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
            cnt_r += 1
    pre_s = s
    pre_d = d

for i in range(1,N+1):
    ans *= i
    ans %= mod
if cnt_l!=N:
    print(0)
    exit()

print(ans)

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
S = list(input())

if S[0]!='B' or S[-1]!='B':
    print(0)
    exit()

mod = 10**9 + 7
cnt_l = 1
pre_s = 'B'
pre_d = 1 #1がleft
ans = 1

for i in range(1,N*2):
    s = S[i]

    if pre_s==s:
        if pre_d==1:
            d = 0
            ans *= (cnt_l*2 - i)%mod #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
        else:
            d = 1
            cnt_l += 1
    else:
        if pre_d==1:
            d = 1
            cnt_l += 1
        else:
            d = 0
            ans *= (cnt_l*2 - i)%mod #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
    pre_s = s
    pre_d = d

for i in range(1,N+1):
    ans *= i
    ans %= mod
if cnt_l!=N:
    print(0)
    exit()

print(ans)

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
S = list(input())

if S[0]!='B' or S[-1]!='B':
    print(0)
    exit()

mod = 10**9 + 7
cnt_l = 1
pre_s = 'B'
pre_d = 1 #1がleft
ans = 1

for i in range(1,N*2):
    s = S[i]

    if pre_s==s:
        if pre_d==1:
            d = 0
            ans *= (cnt_l*2 - i)%mod #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
        else:
            d = 1
            cnt_l += 1
    else:
        if pre_d==1:
            d = 1
            cnt_l += 1
        else:
            d = 0
            ans *= (cnt_l*2 - i)%mod #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
    pre_s = s
    pre_d = d
import math
k = math.factorial(N)
if cnt_l!=N:
    print(0)
    exit()

print(ans*k%mod)

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
S = list(input())

if S[0]!='B' or S[-1]!='B':
    print(0)
    exit()

mod = 10**9 + 7
cnt_l = 1
pre_s = 'B'
pre_d = 1 #1がleft
ans = 1

for i in range(1,N*2):
    s = S[i]

    if pre_s==s:
        if pre_d==1:
            d = 0
            ans *= (cnt_l*2 - i)%mod #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
        else:
            d = 1
            cnt_l += 1
    else:
        if pre_d==1:
            d = 1
            cnt_l += 1
        else:
            d = 0
            ans *= (cnt_l*2 - i)%mod #ここまでに出てきたLeftの総数 - ここまでに出てきたrightの総数
    pre_s = s
    pre_d = d
import math
k = math.factorial(N)
if cnt_l!=N:
    print(0)
    exit()

print(ans*k%mod)

Fixed submission:
import math
n = int(input())
S = input()
 
RL = [None]*2*n
 
for i in range(2*n):
    if i % 2 == 0:
        if S[i] == 'B':
            RL[i] = 'L'
        else:
            RL[i] = 'R'
 
    else:
        if S[i] == 'B':
            RL[i] = 'R'
        else:
            RL[i] = 'L'
 
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit()
 
left = 0
count = 1
mod = 10**9 + 7
 
for check in RL:
    if check == 'L':
        left += 1
    else:
        count = count * left % mod
        left -= 1
 
    if left < 0:
        print(0)
        exit()
 
count = count * math.factorial(n) % mod
 
if left != 0:
    print(0)
else:
    print(count)