Problem:


Score : 500 points

Problem Statement

There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.

The color of the i-th square from the left is black if the i-th character of S is B, and white if that character is W.

You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.  

Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.

Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.

Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.

Constraints

1 \leq N \leq 10^5

|S| = 2N

Each character of S is B or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.

Sample Input 1

2
BWWB

Sample Output 1

4

There are four ways to make all the squares white, as follows:

Choose Squares 1, 3 in the first operation, and choose Squares 2, 4 in the second operation.

Choose Squares 2, 4 in the first operation, and choose Squares 1, 3 in the second operation.

Choose Squares 1, 4 in the first operation, and choose Squares 2, 3 in the second operation.

Choose Squares 2, 3 in the first operation, and choose Squares 1, 4 in the second operation.

Sample Input 2

4
BWBBWWWB

Sample Output 2

288

Sample Input 3

5
WWWWWWWWWW

Sample Output 3

0



Sample input:
2
BWWB


Sample output:
4

Submission status: Wrong Answer

Solution code:
from math import factorial
n = int(input())
inp = list(input())
if inp[0] == 'W' or inp[-1] == 'W':
    print(0)
    exit(0)
arr = ['L']
swap_dict = {'L': 'R', 'R': 'L'}
for i in range(1, 2 * n):
    arr.append(arr[i - 1] if inp[i] != inp[i - 1] else swap_dict[arr[i - 1]])
if arr.count('L') != arr.count('R'):
    print(0)
    exit(0)
cnt_tmp = 0
cnt_swap = 1
for a in arr:
    if a == 'L':
        cnt_tmp += 1
    else:
        cnt_swap *= cnt_tmp
        cnt_tmp -= 1
print(cnt_swap * factorial(n))

Submission status: Wrong Answer

Solution code:
import sys, math
n = int(input())
s = list(input())
store = 0
ans = 1

if s[0] == "W":
    print(0)
    sys.exit()

for i in range(2*n):
    if i % 2 == 0:
        if s[i] == "B":
            s[i] = "W"
        else:
            s[i] = "B"

for i in range(2*n):
    if s[i] == "W":
        store += 1
    else:
        ans *= store
        store -= 1

print(ans*math.factorial(n))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


def judge(bw, lr):
    if bw[0] != "B" or bw[-1] != "B":
        return False

    elif lr.count("L") != lr.count("R"):
        return False

    else:
        return True


def fact(num, mod):
    if num == 1:
        return 1
    else:
        return num * fact(num-1, mod) % mod

n = ni()
bw = lc()
MOD = 10**9 + 7

# 各マスの左端or右端を判定
lr = ['L']
for i in range(2*n-1):
    if bw[i+1] == bw[i]:
        if lr[-1] == "L":
            lr.append("R")
        else:
            lr.append("L")

    else:
        if lr[-1] == "L":
            lr.append("L")
        else:
            lr.append("R")

# 答えが存在するか判定
if judge(bw, lr):
    cnt = 0
    ans = 1
    for lri in lr:
        if lri == "L":
            cnt += 1
        else:
            ans *= cnt
            ans %= MOD
            cnt -= 1

    print(ans * fact(n, MOD))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import math
import os
N = int(input())
S = input()

operations = ["L" for i in range(len(S))]
operations[len(S)-1] = "R"

for i in range(1, len(S)):
    if S[i-1] == S[i]:
        if operations[i-1] == "L":
            operations[i] = "R"
        else:
            operations[i] = "L"
    else:
        operations[i] = operations[i-1]
if S[0] == "W" or S[-1] == "W" or operations[-1] == "L":
    print(0)
    os.sys.exit()

ans = 1
cnt_r = 0
cnt_l = 0
for ope in operations:
    if ope == "R":
        ans *= cnt_l - cnt_r
        cnt_r += 1
    else:
        cnt_l += 1
if cnt_l != cnt_r:
    print(0)
    os.sys.exit()
print(ans*math.factorial(N))

Submission status: Wrong Answer

Solution code:
import math
import os
N = int(input())
S = input()

operations = ["L" for i in range(len(S))]
operations[len(S)-1] = "R"

for i in range(1, len(S)):
    if S[i-1] == S[i]:
        if operations[i-1] == "L":
            operations[i] = "R"
        else:
            operations[i] = "L"
    else:
        operations[i] = operations[i-1]
if S[0] == "W" or S[-1] == "W":
    print(0)
    os.sys.exit()

ans = 1
cnt_r = 0
cnt_l = 0
for ope in operations:
    if ope == "R":
        ans *= cnt_l - cnt_r
        cnt_r += 1
    else:
        cnt_l += 1
if cnt_l != cnt_r:
    print(0)
    os.sys.exit()
print(ans*math.factorial(N))

Submission status: Wrong Answer

Solution code:
def f_c():
    n = int(input())
    s = input()

    stack, c = 1, 1
    for si in s[1:]:
        if si=="B":
            if stack%2==0:
                stack += 1
            else:
                c *= stack
                stack -= 1
        else:
            if stack%2==0:
                c *= stack
                stack -= 1
            else:
                stack += 1
    if stack!=0 | c==0:
        print(0)
    else:
        for i in range(1, n+1):
            c*=i
        print(c%(10**9+7))

if __name__ == "__main__":
    f_c()

Submission status: Wrong Answer

Solution code:
def f_c():
    n = int(input())
    s = input()

    stack, c = 1, 1
    for si in s[1:]:
        if si=="B":
            if stack%2==0:
                stack += 1
            else:
                c *= stack
                stack -= 1
        else:
            if stack%2==0:
                c *= stack
                stack -= 1
            else:
                stack += 1
        if stack<0:
            break
    if stack!=0 | c==0:
        print(0)
    else:
        for i in range(1, n+1):
            c*=i
        print(c%(10**9+7))

if __name__ == "__main__":
    f_c()

Submission status: Wrong Answer

Solution code:
def f_c():
    import math
    n = int(input())
    s = input()

    stack, c = 1, 1
    for i in range(1, n*2):
        if s[i]=="B":
            if stack%2==0:
                stack += 1
            else:
                c *= stack
                stack -= 1
        else:
            if stack%2==0:
                c *= stack
                stack -= 1
            else:
                stack += 1
        if stack<0:
            break
    o = 0 if stack!=0 else c*math.factorial(n)
    print(o)

if __name__ == "__main__":
    f_c()

Submission status: Wrong Answer

Solution code:
import math
n=int(input())*2
S=list(input())
# Lを1、Rを0としてDP
dp=[0]*(n)
dp[0]=1
if S[0]=='W' or S[-1]=='W':
	print(0)
	exit()
for i in range(1,n):
	# print(S,i)
	if S[i]==S[i-1]:
		dp[i]=0 if dp[i-1] else 1
	else:
		dp[i]=1 if dp[i-1] else 0

count=1
counter=[0]*2
for i in range(n):
	if dp[i]==0:
		count*=counter[1]-counter[0]
		count%=10**9+7
	counter[dp[i]]+=1
if counter[0]!=counter[1]:
	print(0)
	exit()

print((count*math.factorial(n//2))%10**9+7)
# print(dp)

Submission status: Wrong Answer

Solution code:
import math
n=int(input())*2
S=list(input())
# Lを1、Rを0としてDP
dp=[0]*(n)
dp[0]=1
if S[0]=='W' or S[-1]=='W':
	print(0)
	exit()
for i in range(1,n):
	# print(S,i)
	if S[i]==S[i-1]:
		dp[i]=0 if dp[i-1] else 1
	else:
		dp[i]=1 if dp[i-1] else 0

count=1
counter=[0]*2
for i in range(n):
	if dp[i]==0:
		count*=counter[1]-counter[0]
		count%=10**9+7
	counter[dp[i]]+=1
if counter[0]!=counter[1]:
	print(0)
	exit()

print(count*math.factorial(n//2))
# print(dp)

Submission status: Wrong Answer

Solution code:
import math
n=int(input())*2
S=list(input())
# Lを1、Rを0としてDP
dp=[0]*(n)
dp[0]=1
if S[0]=='W' or S[-1]=='W':
	print(0)
	exit()
for i in range(1,n):
	# print(S,i)
	if S[i]==S[i-1]:
		dp[i]=0 if dp[i-1] else 1
	else:
		dp[i]=1 if dp[i-1] else 0

count=1
counter=[0]*2
for i in range(n):
	if dp[i]==0:
		count*=counter[1]-counter[0]
		count%=10**9+7
	counter[dp[i]]+=1

print(count*math.factorial(n//2))
# print(dp)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
st=''
mod=10**9+7
for i in range(2*n):
    if i%2==0:
        if s[i]=='B':
            st+='W'
        else:
            st+='B'
    else:
        st+=s[i]

cnt=0
ans=1
for i in range(2*n):
    if st[i]=='W':
        cnt+=1
    else:
        ans*=cnt
        ans%=mod
        cnt-=1
for i in range(1,n+1):
    ans*=i
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9+7

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

if S[0] == "W" or S[-2] == "W":
    print(0)
    sys.exit()

LR = [0 for i in range(2*N)]
count0 = 0
ans = 1
for i in range(1, 2*N):
    if LR[i-1] == 0:
        if S[i] == S[i-1]:
            LR[i] = 1
        else:
            LR[i] = 0
    else:
        if S[i] == S[i-1]:
            LR[i] = 0
        else:
            LR[i] = 1

    if LR[i] == 0:
        count0 += 1
    else:
        ans *= count0 % MOD
        count0 -= 1

if LR.count(0) != N:
    print(0)
    sys.exit()

def factorial(N, mod):
    if N == 0 or N == 1:
        return 1
    else:
        return N * factorial(N-1, mod) % mod

print(ans*factorial(N,MOD)%MOD)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import math

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

if S[0] == "W" or S[-2] == "W":
    print(0)
    sys.exit()

LR = [0]
for i in range(1, 2*N):
    if LR[i-1] == 0:
        if S[i] == S[i-1]:
            LR.append(1)
        else:
            LR.append(0)
    else:
        if S[i] == S[i-1]:
            LR.append(0)
        else:
            LR.append(1)

if LR.count(0) != N:
    print(0)
    sys.exit()

count0 = 0
ans = 1
for i in range(2*N):
    if LR[i] == 0:
        count0 += 1
    else:
        ans *= count0
        count0 -= 1

print(ans*math.factorial(N))

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
s = input()
MOD = 10**9+7

lr = [False] * 2*n
lr[0] = True # True:Left False:Right
for i in range(1,2*n):
    lr[i] = not lr[i-1] if s[i-1] == s[i] else lr[i-1]

if s[0] == 'W':
    lr = lr[1:]
if s[-1] == 'W':
    lr = lr[:-1]

ans = 1
lCnt = 0
for i in range(len(lr)):
    if not lr[i]: # R
        ans *= lCnt
        lCnt -= 1
    else:       # L
        lCnt += 1
print(math.factorial(n) * ans % MOD)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
s = input()

lr = [False] * 2*n
lr[0] = True # True:Left False:Right
for i in range(1,2*n):
    lr[i] = not lr[i-1] if s[i-1] == s[i] else lr[i-1]

if s[0] == 'W':
    lr = lr[1:]
if s[-1] == 'W':
    lr = lr[:-1]

ans = 1
lCnt = 0
for i in range(len(lr)):
    if not lr[i]: # R
        ans *= lCnt
        lCnt -= 1
    else:       # L
        lCnt += 1
print(math.factorial(n) * ans)

Submission status: Wrong Answer

Solution code:
mod = int(1e9+7)

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

a = [1 if s[i] == 'B' else 0 for i in range(len(s))]
ans = 1

if a[0] == 0:
    print(0)
    exit()

c = 1

for i in range(1,len(s)):
    if (a[i] + c) % 2 == 1:
        c += 1
    else:
        if a[i-1] % 2 == 1 and c == 1:
            print(0)
            exit()
        ans = (ans*c) % mod
        c -= 1

if c != 0:
    print(0)
    exit()

for i in range(n):
    ans = (ans*(i+1)) % mod

print(ans)

Submission status: Wrong Answer

Solution code:
mod = int(1e9+7)

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

a = [1 if s[i] == 'B' else 0 for i in range(len(s))] + [0]
ans = 1

if a[0] == 0:
    print(0)
    exit()

c = 1

for i in range(1,len(s)):
    if (a[i] + c) % 2 == 1:
        c += 1
    else:
        if a[i-1] % 2 == 1 and c == 1:
            print(0)
            exit()
        ans = (ans*c) % mod
        c -= 1

for i in range(n):
    ans = (ans*(i+1)) % mod

print(ans)

Submission status: Wrong Answer

Solution code:
mod = int(1e9+7)

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

a = [1 if s[i] == 'B' else 0 for i in range(len(s))] + [0]
ans = 1

c = 0

for i in range(len(s)):
    if (a[i] + c) % 2 == 1:
        c += 1
    else:
        if a[0] == 0:
            print(0)
            exit()
        elif a[i-1] % 2 == 1 and c == 1:
            print(0)
            exit()
        ans = (ans*c) % mod
        c -= 1

for i in range(n):
    ans = (ans*(i+1)) % mod

print(ans)

Submission status: Wrong Answer

Solution code:
mod = int(1e9+7)

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

a = [1 if s[i] == 'B' else 0 for i in range(len(s))] + [0]
ans = 1

c = 0

for i in range(len(s)):
    if (a[i] + c) % 2 == 1:
        c += 1
    else:
        if a[i-1] % 2 == 1 and c == 1:
            print(0)
            exit()
        ans = (ans*c) % mod
        c -= 1

for i in range(n):
    ans = (ans*(i+1)) % mod

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)


n = int(input())
s = input().rstrip()
MOD = 10**9+7

cnt = 0
point = 1
for i, x in enumerate(s):
    if (x == 'B' and i % 2 == 0) or (x == 'W' and i % 2 != 0):
        cnt += 1
    else:
        point *= cnt
        cnt -= 1


# factorial
for x in range(1, n+1):
    point *= x
    point %= MOD

print(point)

Submission status: Wrong Answer

Solution code:
from math import factorial
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)


n = int(input())
s = input().rstrip()
MOD = 10**9+7

cnt = 0
point = 1
for i, x in enumerate(s):
    if (x == 'B' and i % 2 == 0) or (x == 'W' and i % 2 != 0):
        cnt += 1
    else:
        point *= cnt
        cnt -= 1

print(point*factorial(n) % MOD)

Submission status: Wrong Answer

Solution code:
from math import factorial

n = int(input())
s = input()
MOD = 10**9+7

cnt = 0
point = 1
for i, x in enumerate(s):
    if (x == 'B' and i % 2 == 0) or (x == 'W' and i % 2 != 0):
        cnt += 1
    else:
        cnt %= MOD
        point *= cnt
        cnt -= 1

print(point*factorial(n) % MOD)

Submission status: Wrong Answer

Solution code:
from math import factorial

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


MOD = 10**9+7

cnt = 0
point = 1
for i, x in enumerate(s):
    if i % 2 == 0:
        if x == 'B':
            cnt += 1
        else:
            point *= cnt
            cnt -= 1
    else:
        if x == 'B':
            point *= cnt
            cnt -= 1
        else:
            cnt += 1


print(point*factorial(n) % MOD)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
MOD = 10**9+7

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

if s[0]=="W" or s[-1]=="W":
    print(0)
    exit()

lr = ["L"]
for i in range(1,2*n):
    if s[i-1]==s[i]:
        if lr[i-1]=="L":
            lr.append("R")
        else:
            lr.append("L")
    else:
        lr.append(lr[i-1])

la = [0 for _ in range(2*n+1)]
ra = [0 for _ in range(2*n+1)]
for i in range(2*n):
    if lr[i]=="L":
        la[i+1] = la[i]+1
        ra[i+1] = ra[i]
    else:
        la[i+1] = la[i]
        ra[i+1] = ra[i]+1

res = 1
for i in range(2*n):
    if lr[i]=="R":
        res *= la[i]-ra[i]
        res %= MOD

for i in range(2,n+1):
    res = (res*i)%MOD

print(res)

Submission status: Wrong Answer

Solution code:
from math import factorial

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


MOD = 10**9+7


lr = ''
for i, x in enumerate(s):
    if i % 2 == 0:
        if x == 'B':
            lr += 'L'
        else:
            lr += 'R'
    else:
        if x == 'B':
            lr += 'R'
        else:
            lr += 'L'

cnt = 0
point = 1
for x in lr:
    if x == 'L':
        cnt += 1
    else:
        point *= cnt
        cnt -= 1


print(point*factorial(n) % MOD)

Submission status: Wrong Answer

Solution code:
N=input()
S=raw_input()

L=[]

for i,x in enumerate(S):
		if x=="B": n=1
		elif x=="W": n=-1
		if i%2==1: n*=-1
		L.append(n)


mod=10**9+7

ans=1
t=0
for x in L:
	if x==-1:
		ans*=t
		ans%=mod
	t+=x


for n in range(1,N+1):
	ans*=n
	ans%=mod

print ans

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
Q = 10**9+7
def main():
    N = int( input())
    S = input()
    left = 0
    ans = 1
    for s in S:
        if s == "B":
            if left%2 == 0:
                left += 1
            else:
                # if left == 0:
                #     print(0)
                #     return
                ans *= left
                left -= 1
        else:
            if left%2 == 1:
                left += 1
            else:
                if left == 0:
                    print(0)
                    return
                ans *= left
                left -= 1
        ans %= Q
    if left > 0:
        print(0)
        return
    for i in range(1,N+1):
        ans *= i
        ans %= Q
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
Q = 10**9+7
def main():
    N = int( input())
    S = input()
    left = 0
    ans = 1
    for s in S:
        if s == "B":
            if left%2 == 0:
                left += 1
            else:
                if left == 0:
                    print(0)
                    return
                ans *= left
                left -= 1
        else:
            if left%2 == 1:
                left += 1
            else:
                if left == 0:
                    print(0)
                    return
                ans *= left
                left -= 1
        ans %= Q
    if left > 0:
        print(0)
        return
    for i in range(1,N+1):
        ans *= i
        ans %= Q
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
Q = 10**9+7
def main():
    N = int( input())
    S = input()
    left = 0
    ans = 1
    for s in S:
        if s == "B":
            if left%2 == 0:
                left += 1
            else:
                if left == 0:
                    print(0)
                    return
                ans *= left
                left -= 1
        else:
            if left%2 == 1:
                left += 1
            else:
                if left == 0:
                    print(0)
                    return
                ans *= left
                left -= 1
        ans %= Q
    if left != 0:
        print(0)
        return
    for i in range(1,N+1):
        ans *= i
        ans %= Q
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#import sys
#input = sys.stdin.readline
Q = 10**9+7
def main():
    N = int( input())
    S = input()
    left = 0
    ans = 1
    for s in S:
        if s == "B":
            if left%2 == 0:
                left += 1
            else:
                if left == 0:
                    print(0)
                    return
                ans *= left
                left -= 1
        else:
            if left%2 == 1:
                left += 1
            else:
                if left == 0:
                    print(0)
                    return
                ans *= left
                left -= 1
        ans %= Q
    for i in range(1,N+1):
        ans *= i
        ans %= Q
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
import random
import sys
from functools import lru_cache
import collections
import math

def main():
    mod = 10**7 + 9
    n = int(input())
    s = list(input())
    if s[0] == 'W' or s[-1] == 'W':
        print(0)
        sys.exit()

    for i in range(len(s)):
        if not i%2:
            if s[i] == 'W':
                s[i] = 'B'
            else:
                s[i] = 'W'
    
    
    c = collections.Counter(s)

    if not  c['B'] == c['W']:
        print(0)
        sys.exit()

    bc, wc = 0, 0
    ret = 1
    for i in range(len(s)):
        if s[i] == 'W':
            wc += 1
        else:
            ret *= wc
            wc -= 1
        
        ret
    
    print((ret*math.factorial(c['W']))%mod)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
import random
import sys
from functools import lru_cache
import collections
import math

def main():
    mod = 10**7 + 9
    n = int(input())
    s = list(input())
    if s[0] == 'W' or s[-1] == 'W':
        print(0)
        sys.exit()

    for i in range(len(s)):
        if not i%2:
            if s[i] == 'W':
                s[i] = 'B'
            else:
                s[i] = 'W'
    
    
    c = collections.Counter(s)

    if not  c['B'] == c['W']:
        print(0)
        sys.exit()

    bc, wc = 0, 0
    ret = 1
    for i in range(len(s)):
        if s[i] == 'W':
            wc += 1
        else:
            ret *= wc
            wc -= 1
        
        ret %= mod
    
    print(ret*math.factorial(c['W']))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def main():
    n=int(input())
    s=input()
    MOD=10**9+7
    perm=1
    for i in range(1,n+1):
        perm=perm*i%MOD
    ans=1
    dp=[-1]*(2*n)

    dp[0]=1#L
    count=0
    for i in range(1,2*n):
        if s[i]==s[i-1]:
            dp[i]=1-dp[i-1]
        else:
            dp[i]=dp[i-1]
        if dp[i]==1:
            count+=1
        else:
            ans*=count
            count-=1
    if s[0]=='W' or s[-1]=='W' or sum(dp)!=n:
        print(0)
        sys.exit()
    #print(dp)
    
    
    print(ans*perm%MOD)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n=int(input())
    s=input()
    MOD=10**9+7
    perm=1
    for i in range(1,n+1):
        perm=perm*i%MOD
    ans=1
    dp=[0]*(2*n+1)

    for i in range(1,2*n+1):
        x=dp[i-1]
        if (x+1)%2==(s[i-1]=='B'):
            dp[i]=(x+1)%MOD
        else:
            ans*=x%MOD
            dp[i]=(x-1)%MOD
    #print(dp)
    print(ans*perm%MOD)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n=int(input())
    s=input()
    MOD=10**9+7
    perm=[1]*(n+1)
    for i in range(1,n+1):
        perm[i]=perm[i-1]*i%MOD
    ans=1
    dp=[0]*(2*n+1)

    for i in range(1,2*n+1):
        x=dp[i-1]
        if (x+1)%2==(s[i-1]=='B'):
            dp[i]=(x+1)%MOD
        else:
            ans*=x%MOD
            dp[i]=(x-1)%MOD
    #print(dp)
    print(ans*perm[n]%MOD)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
from math import factorial


cnt = 0

ans = 1
mod = 10 ** 9 + 7
for i in range(2 * N):
    s = S[i]
    if s == "W":
        if cnt == 0:
            print(0)
            exit()
        else:
            if cnt % 2 == 0:
                ans *= cnt
                ans %= mod
                cnt -= 1
            else:
                cnt += 1
    else:
        if cnt % 2 == 0:
            cnt += 1
        else:
            ans *= cnt
            ans %= mod
            cnt -= 1

ans *= factorial(N)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()

ans = 1
inf = 10 ** 9 + 7
d = 'L'
l_count = 1
r_count = 0
for i in range(1, 2 * N):
    if S[i] != S[i - 1]:
        if d == 'R':
            ans *= l_count - r_count
            ans %= inf
            r_count += 1
        else:
            l_count += 1
    else:
        if d == 'R':
            d = 'L'
            l_count += 1
        else:
            ans *= l_count - r_count
            ans %= inf
            d = 'R'
            r_count += 1

for j in range(1, N + 1):
    ans *= j
    ans %= inf

print(ans % inf)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()

ans = 1
inf = 10 ** 9 + 7
d = 'L'
l_count = 1
r_count = 0
for i in range(1, 2 * N):
    if S[i] != S[i - 1]:
        l_count += 1
        if d == 'R':
            ans *= l_count - r_count
            ans %= inf
    else:
        if d == 'R':
            d = 'L'
        else:
            ans *= l_count - r_count
            ans %= inf
            d = 'R'
        r_count += 1

for j in range(1, N + 1):
    ans *= j
    ans %= inf

print(ans % inf)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()

ans = 1
inf = 10 ** 9 + 7
d = 'L'
l_count = 1
r_count = 0
for i in range(1, 2 * N):
    if S[i] == 'R':
        ans *= l_count - r_count
        ans %= inf
    if S[i] != S[i - 1]:
        l_count += 1
    else:
        if d == 'R':
            d = 'L'
        else:
            d = 'R'
        r_count += 1

for j in range(1, N + 1):
    ans *= j
    ans %= inf

print(ans % inf)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()

ans = 1
inf = 10 ** 9 + 7
d = 'L'
l_count = 1
r_count = 0
for i in range(1, 2 * N):
    if S[i] != S[i - 1]:
        l_count += 1
    else:
        if d == 'R':
            d = 'L'
        else:
            d = 'R'
        r_count += 1
    if d == 'R':
        ans *= l_count - r_count
        ans %= inf

for j in range(1, N + 1):
    ans *= j
    ans %= inf

print(ans % inf)

Submission status: Wrong Answer

Solution code:
from math import factorial
N = int(input())
S = input()
MOD = 10 ** 9 + 7

LR = [True]  # Left is True
for i in range(N*2-1):
    LR += [LR[-1]] if S[i+1] != S[i] else [not LR[-1]]

# calc lr combination
cmb = 1
cr = 0
for i in range(N*2-1, -1, -1):
    if not LR[i]:
        cr += 1
    else:
        cmb *= cr
        cmb %= MOD
        cr -= 1

print((factorial(N) * cmb) % MOD)

Submission status: Wrong Answer

Solution code:
import sys
import math
N=int(input())
S=input()
D=[0]*2*N
D[0]=0
D[-1]=1

if S[0]=='W' or S[-1]=='W':
    print(0)
    sys.exit()
    
for i in range(1,2*N-1):
    if S[i-1]==S[i]:
        D[i]=(D[i-1]+1)%2
    else:
        D[i]=D[i-1]

    
cnt=0
ans=1
for i in range(2*N):
    if D[i]==1:
        ans*=i-2*cnt
    cnt+=D[i]

print(ans*math.factorial(N))

Submission status: Wrong Answer

Solution code:
from collections import Counter
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class FactMod():
    '''
    modの値が素数の時のfactと組み合わせを求める
    フェルマーの小定理を用いているため、modが素数以外の時は使えない
    '''

    def __init__(self, n, mod):
        '''
        コンストラクタ
        f:nまでの i!の値を　配列に入れる
        inv: (i!)^-1　の値を配列に入れる
        '''
        self.mod = mod
        self.f = [1]*(n+1)
        for i in range(1, n+1):
            self.f[i] = self.f[i-1]*i % mod

        self.inv = [pow(self.f[-1], mod-2, mod)]
        for i in range(1, n+1)[::-1]:
            self.inv.append(self.inv[-1]*i % mod)
        self.inv.reverse()

    def fact(self, n):
        '''
        n!の値を返す
        '''
        return self.f[n]

    def comb(self, n, r):
        '''
        nCrの値を返す
        '''
        ret = self.f[n] * self.inv[n-r]*self.inv[r]
        ret %= self.mod
        return ret

    def perm(self, n, r):
        """
        nPrの値を返す
        """
        ret = self.f[n] * self.inv[n-r]
        ret %= self.mod
        return ret

    def fact(self, n):
        return self.f[n]


MOD = 10**9+7
N = int(input())
S = input()[:-1]
F = FactMod(N+1, MOD)

cnt_W = 0
cnt = 0
ans = 1
for i, c in enumerate(S):
    if c == "B":
        tmp = 1
    else:
        tmp = 0
    if i % 2 == 0:
        tmp ^= 1

    if tmp == 0:
        cnt += 1
    else:
        ans *= cnt
        ans %= MOD
        cnt -= 1
        cnt_W += 1
if cnt != N:
    ans = 0
else:
    ans *= F.fact(N)
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class FactMod():
    '''
    modの値が素数の時のfactと組み合わせを求める
    フェルマーの小定理を用いているため、modが素数以外の時は使えない
    '''

    def __init__(self, n, mod):
        '''
        コンストラクタ
        f:nまでの i!の値を　配列に入れる
        inv: (i!)^-1　の値を配列に入れる
        '''
        self.mod = mod
        self.f = [1]*(n+1)
        for i in range(1, n+1):
            self.f[i] = self.f[i-1]*i % mod

        self.inv = [pow(self.f[-1], mod-2, mod)]
        for i in range(1, n+1)[::-1]:
            self.inv.append(self.inv[-1]*i % mod)
        self.inv.reverse()

    def fact(self, n):
        '''
        n!の値を返す
        '''
        return self.f[n]

    def comb(self, n, r):
        '''
        nCrの値を返す
        '''
        ret = self.f[n] * self.inv[n-r]*self.inv[r]
        ret %= self.mod
        return ret

    def perm(self, n, r):
        """
        nPrの値を返す
        """
        ret = self.f[n] * self.inv[n-r]
        ret %= self.mod
        return ret

    def fact(self, n):
        return self.f[n]


MOD = 10**9+7
N = int(input())
S = input()[:-1]
F = FactMod(N+1, MOD)

cnt_W = 0
cnt = 0
ans = 1
for i, c in enumerate(S):
    if c == "B":
        tmp = 1
    else:
        tmp = 0
    if i % 2 == 0:
        tmp ^= 1

    if tmp == 0:
        cnt += 1
    else:
        ans *= cnt
        ans %= MOD
        cnt -= 1
        cnt_W += 1
ans *= F.fact(N)
ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7
n = int(input())
s = input()

t = []
for i in range(2*n):
    if i % 2 == 0:
        t.append(0 if s[i] == 'B' else 1)
    else:
        t.append(0 if s[i] == 'W' else 1)

# print(t)
ans = 1
l = r = 0
for i in range(2*n):
    if t[i] == 1:
        ans *= max(l - r, 0)
        ans %= MOD

    if t[i] == 0:
        l += 1
    else:
        r += 1

for i in range(1, n+1):
    ans *= i
    ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7
n = int(input())
s = input()

t = []
for i in range(2*n):
    if i % 2 == 0:
        t.append(0 if s[i] == 'B' else 1)
    else:
        t.append(0 if s[i] == 'W' else 1)

ans = 1
l = r = 0
for i in range(2*n):
    if i > 0 and t[i] == 1:
        ans *= max(l - r, 0)
        ans %= MOD

    if t[i] == 0:
        l += 1
    else:
        r += 1

for i in range(1, n+1):
    ans *= i
    ans %= MOD

print(ans)