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:
n=int(input())
s=input()

LR="L"

for i in range(1,len(s)):
  if s[i-1]==s[i]:LR+="R" if LR[i-1]=="L" else "L"
  else:LR+="R" if LR[i-1]=="R" else "L"

if s[0]=="W" or s[-1]=="W" or (s.count("W")!=n and n>1):
  print(0)
  exit()

count=1
for i in range(1,len(s)):
  if LR[i]=="R":
    count=count*(LR[:i].count("L")-LR[:i].count("R"))
    #print(count,LR[:i].count("L"),LR[:i].count("R"))

x=1
for i in range(1,n+1):
  x=x*i

#print(LR,count,x)
print(count*x)

Submission status: Wrong Answer

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

LR="L"

for i in range(1,len(s)):
  if s[i-1]==s[i]:LR+="R" if LR[i-1]=="L" else "L"
  else:LR+="R" if LR[i-1]=="R" else "L"

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

count=1
for i in range(1,len(s)):
  if LR[i]=="R":
    count=count*(LR[:i].count("L")-LR[:i].count("R"))
 #   print(count,LR[:i].count("L"),LR[:i].count("R"))

x=1
for i in range(1,n+1):
  x=x*i

#print(LR,count,x)
print(count*x)

Submission status: Wrong Answer

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

MOD = 10**9
TO_INT = {'B': 1, 'W': -1}
BW = S[:]
BW = [TO_INT[s] for s in BW]

if BW[0] == -1 or BW[-1] == -1:
    print(0)
else:
    LR = [1] # {'L': 1, 'R': -1}
    for i in range(1, 2*N):
        if BW[i-1] != BW[i]:
            LR.append(LR[i-1])
        else:
            LR.append(-1*LR[i-1])
    if sum(LR) != 0:
        print(0)
    elif LR[-1] == 1:
        print(0)
    else:
        cnt = 0
        ans = 1
        for n in LR:
            if n == 1:
                cnt+=1
            else:
                ans = ans * cnt % MOD
                cnt -= 1
        import math
        print(ans*math.factorial(N)% MOD)

Submission status: Wrong Answer

Solution code:
M=10**9+7
n,s=open(0)
n=int(n)
a=l=1
r=f=0
for b,c in zip(s,s[1:-1]):f^=b==c;a=a*(l-r)**f%M;r+=f;l+=f^1
while n:a=a*n%M;n-=1
print(a*(l==r))

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
s = [0 if i == "W" else 1 for i in list(input())]
cnt = 0
c = 1
for i in range(n * 2):
    if (s[i] + cnt) % 2 == 0:
        cnt += 1
    else:
        c *= (cnt if cnt != 0 else 1)
        cnt -= 1
    if cnt < 0:
        print(0)
        exit()
if cnt != 0:
    print(0)
else:
    print(c * math.factorial(n))

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
s = input()
cnt = 0
c = 1
for i in range(n * 2):
    if int(1 if s[i] == "B" else 0) != cnt % 2:
        cnt += 1
    else:
        c *= (cnt if cnt != 0 else 1)
        cnt -= 1
    if cnt < 0:
        print(0)
        exit()
if cnt != 0:
    print(0)
else:
    print(c * math.factorial(n))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
count = 0
ans = 1
m = 10**9 + 7
 
for i in range(N):
  if S[2*i] == "W":
    ans *= (i - (count*2))
    count += 1
  if S[2*i + 1] == "B":
    ans *= (i - (count*2))
    count += 1
if count != N:
  print(0)
else:
  for o in range(1, N+1):
    ans *= o
    ans %= m
  print(ans % m)

Submission status: Wrong Answer

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

MOD = 10**9
TO_INT = {'B': 1, 'W': -1}
BW = S[:]
BW = [TO_INT[s] for s in BW]

if BW[0] == -1:
    print(0)
else:
    LR = [1] # {'L': 1, 'R': -1}
    for i in range(1, 2*N):
        if BW[i-1] != BW[i]:
            LR.append(LR[i-1])
        else:
            LR.append(-1*LR[i-1])
    if sum(LR) != 0:
        print(0)
    elif LR[-1] == 1:
        print(0)
    else:
        cnt = 0
        ans = 1
        for n in LR:
            if n == 1:
                cnt+=1
            else:
                ans = ans * cnt % MOD
                cnt -= 1
        import math
        print(ans*math.factorial(N)% MOD)

Submission status: Wrong Answer

Solution code:
M = 10**9+7
N = int(input())
S = list(input())
s = [0]*(2*N)

#0ならW、1ならB
for i in range(2*N):
    if S[i] == "B":
        s[i] = 1

#端が0なら0を返す
if s[0] == 0 or s[2*N-1] == 0:
    print(0)
    exit()

ans = 0
lis = [0]+[1]*(2*N-1) #0なら左、1なら右
#全て0にする

for j in range(1,2*N):
    if s[j-1] != s[j]:
        lis[j] = lis[j-1]
    else:
        lis[j] = lis[j-1] ^ 1

#左と右の数が違ったら0を返す
if lis.count(0) != N:
    print(0)
    exit()

#"右"が選べる"左"の選択肢を掛けていく
cnt = 1
ans = 1
for k in range(1,2*N):
    if lis[k] == 0:
        
        cnt += 1
    else:
        ans *= cnt
        cnt -= 1
    print(cnt)

#N!をかける
for l in range(1,N+1):
    ans *= l
    ans %= M
print(ans)

Submission status: Wrong Answer

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

MOD = 10**9
TO_INT = {'B': 1, 'W': -1}
BW = S[:]
BW = [TO_INT[s] for s in BW]

if BW[0] == -1:
    print(0)
else:
    LR = [1] # {'L': 1, 'R': -1}
    for i in range(1, 2*N):
        if BW[i-1] != BW[i]:
            LR.append(LR[i-1])
        else:
            LR.append(-1*LR[i-1])
    if sum(LR) != 0:
        print(0)
    else:
        cnt = 0
        ans = 1
        for n in LR:
            if n == 1:
                cnt+=1
            else:
                ans = ans * cnt % MOD
                cnt -= 1
        import math
        print(ans*math.factorial(N)% MOD)

Submission status: Wrong Answer

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

MOD = 10**9
TO_INT = {'B': 1, 'W': -1}
BW = S[:]
BW = [TO_INT[s] for s in BW]

if BW[0] == -1:
    print(0)
else:
    LR = [1] # {'L': 1, 'R': -1}
    for i in range(1, 2*N):
        if BW[i-1] != BW[i]:
            LR.append(LR[i-1])
        else:
            LR.append(-1*LR[i-1])
    if sum(LR) != 0:
        print(0)
    #print(LR)
    
    cnt = 0
    ans = 1
    for n in LR:
        if n == 1:
            cnt+=1
        else:
            ans = ans * cnt % MOD
            cnt -= 1
    import math
    print(ans*math.factorial(N)% MOD)

Submission status: Wrong Answer

Solution code:
M = 10**9+7
N = int(input())
S = list(input())
s = [0]*(2*N)

#0ならW、1ならB
for i in range(2*N):
    if S[i] == "B":
        s[i] = 1

#端が0なら0を返す
if s[0] == 0 or s[2*N-1] == 0:
    print(0)
    exit()

ans = 0
lis = [0]+[1]*(2*N-1) #0なら左、1なら右
#全て0にする

for j in range(1,2*N):
    if s[j-1] == s[j]:
        lis[j] = lis[j-1]
    else:
        lis[j] = lis[j-1] ^ 1

#左と右の数が違ったら0を返す
if lis.count(0) != N:
    print(0)
    exit()

#"右"が選べる"左"の選択肢を掛けていく
cnt = 1
ans = 1
for k in range(1,2*N):
    if lis[k] == 0:
        cnt += 1
    else:
        ans *= cnt
        cnt -= 1

#N!をかける
for l in range(1,N+1):
    ans *= l
    ans %= M
print(ans)

Submission status: Wrong Answer

Solution code:
M = 10**9+7
N = int(input())
s = list(input())

#0ならW、1ならB
for i in range(2*N):
    if s[i] == "B":
        s[i] = 1
 
#端が0なら0を返す
if s[0] == 0 or s[2*N-1] == 0:
    print(0)
    exit()
 
ans = 0
lis = [0]+[1]*(2*N-1) #0なら左、1なら右
#全て0にする
 
for j in range(1,2*N):
    if s[j-1] != s[j]:
        lis[j] = lis[j-1]
    else:
        lis[j] = lis[j-1] ^ 1
 
#左と右の数が違ったら0を返す
if lis.count(0) != lis.count(1):
    print(0)
    exit()
 
#"右"が選べる"左"の選択肢を掛けていく
cnt = 1
ans = 1
for k in range(1,2*N):
    if lis[k] == 0:
        cnt += 1
    else:
        ans *= cnt
        cnt -= 1
 
#N!をかける
for l in range(1,N+1):
    ans *= l
    ans %= M
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7

N = int(input())
S = input()
lr = ['L']*(2*N)

for i in range(1,2*N):
    if S[i]==S[i-1]:
        if lr[i-1]=='L':
            lr[i] = 'R'
        else:
            lr[i] = 'L'
    else:
        lr[i] = lr[i-1]

ans = 1
l = 0
for i in range(2*N):
    if lr[i]=='L':
        l += 1
    else:
        ans *= l
        ans %= mod
        l -= 1

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

l = [i for i in range(2*N) if lr[i]=='L']
r = [i for i in range(2*N) if lr[i]=='R']
s = [0]*(2*N+1)
for i in range(2*N):
    if S[i]=='W':
        s[i] = 0
    else:
        s[i] = 1

if len(l)!=len(r):
    ans = 0
else:
    for i in range(N):
        s[l[i]] += 1
        s[r[i]+1] -= 1
    for i in range(2*N):
        s[i+1] += s[i]
        if s[i]%2!=0:
            ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
import math
N =int(input())
S = input()
# S = "".join(["BWWB" for i in range(25*10**3)])
N = len(S)//2
MOD = 10**9+7

L,R = 1,0
r = []
if S[0] != "B":
    print(0)
    exit()
r.append(L)

for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        r.append(r[-1])
    else:
        r.append(1-r[-1])
if sum(r) != N or r[-1] != R:
    print(0)
    exit()

lc = 0
c = 1
for rr in r:
    if rr == R:
        c = (c*lc)%MOD
        lc -=1
    else:
        lc += 1
print(c*math.factorial(N) )

Submission status: Wrong Answer

Solution code:
import math
N =int(input())
S = input()
# S = "".join(["BWWB" for i in range(25*10**3)])
N = len(S)//2

L,R = 1,0
r = []
if S[0] != "B":
    print(0)
    exit()
r.append(L)

for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        r.append(r[-1])
    else:
        r.append(1-r[-1])
if sum(r) != N or r[-1] != R:
    print(0)
    exit()

lc = 0
c = 1
for rr in r:
    if rr == R:
        c *= lc
        lc -=1
    else:
        lc += 1
print(c*math.factorial(N) )

Submission status: Wrong Answer

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

l = 0
ans = 1
for i,c in enumerate(S):
    isr = (i%2) ^ int(c=='W')
    if isr:
        if l:
            ans *= l
            ans %= MOD
            l -= 1
        else:
            print(0)
            exit()
    else:
        l += 1

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

Submission status: Wrong Answer

Solution code:
import sys
import math
from pprint import pprint

def solve(n, s):

    ans = 0
    # if s[0] == "W" or s[2*n-1] == "W":
    #     return ans

    l_c = 0
    r_c = 0
    o_c = 1
    mod = 10**9 + 7
    for s_i in s:
        if s_i == "B" and (l_c - r_c) % 2 == 0:
            l_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 0:
            o_c *= l_c - r_c
            o_c %= mod
            r_c += 1
        elif s_i == "B" and (l_c - r_c) % 2 == 1:
            o_c *= l_c - r_c
            o_c %= mod
            r_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 1:
            l_c += 1
        # print(s_i, l_c, r_c, o_c)
        if l_c - r_c < 0:
            return 0
    # print(l_c, r_c, o_c)

    return (o_c * math.factorial(n)) % mod

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    s = list(sys.stdin.readline().strip())
    print(solve(n, s))

Submission status: Wrong Answer

Solution code:
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 = np.array(values).prod()*math.factorial(n)
print(value%(10**9+7))

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
s = input()
mod = 10**9 +7
if s[0] == "W" or s[-1] == "W":
    print(0)
else:
    d = "L"
    for i in range(1,2*n):
        if s[i-1] == s[i]:
            if d[-1] == "L":
                d += "R"
            else: d+= "L"
        else:
            if d[-1] == "L":
                d += "L"
            else: d += "R"

    count_l = 0
    count_r = 0
    prob = []
    for i in d:
        if i == "L":
            count_l += 1
            prob.append(0)
        else:
            if count_l - count_r <= 0:
                print(0)
                break
            else:
                prob.append(count_l - count_r)
                count_r += 1
    if count_l != count_r:
        print(0)
    else:
        count = 1
        for i in prob: # di= Rとなる各マスについて、Li - Ri通りの選択肢があり、それらの積が操作の場合の数になる。
            if i > 0:
                count = count * i

        ans = count * math.factorial(n) % mod
        print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
from pprint import pprint

def solve(n, s):

    ans = 0
    if s[0] == "W" or s[2*n-1] == "W":
        return ans

    l_c = 0
    r_c = 0
    o_c = 1
    mod = 10**9 + 7
    for s_i in s:
        if s_i == "B" and (l_c - r_c) % 2 == 0:
            l_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 0:
            o_c *= l_c - r_c
            o_c %= mod
            r_c += 1
        elif s_i == "B" and (l_c - r_c) % 2 == 1:
            o_c *= l_c - r_c
            o_c %= mod
            r_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 1:
            l_c += 1
        # print(s_i, l_c, r_c, o_c)
        if l_c - r_c < 0:
            return 0
    # print(l_c, r_c, o_c)

    return (o_c * math.factorial(n)) % mod

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    s = list(sys.stdin.readline().strip())
    print(solve(n, s))

Submission status: Wrong Answer

Solution code:
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)
print(np.array(values).prod()*math.factorial(n)%(10**9+7))

Submission status: Wrong Answer

Solution code:
import sys
import math
from pprint import pprint

def solve(n, s):

    ans = 0
    if s[0] == "W" or s[2*n-1] == "W":
        return ans

    l_c = 0
    r_c = 0
    o_c = 1
    mod = 10**9 + 7
    for s_i in s:
        if s_i == "B" and (l_c - r_c) % 2 == 0:
            l_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 0:
            o_c *= l_c - r_c
            o_c %= mod
            r_c += 1
        elif s_i == "B" and (l_c - r_c) % 2 == 1:
            o_c *= l_c - r_c
            o_c %= mod
            r_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 1:
            l_c += 1
        # print(s_i, l_c, r_c, o_c)
    # print(l_c, r_c, o_c)

    return (o_c * math.factorial(n)) % mod

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    s = list(sys.stdin.readline().strip())
    print(solve(n, s))

Submission status: Wrong Answer

Solution code:
import sys
import math
from pprint import pprint

def solve(n, s):

    ans = 0
    if s[0] == "W" or s[2*n-1] == "W":
        return ans

    l_c = 0
    r_c = 0
    o_c = 1
    mod = 10**9 + 7
    for s_i in s:
        if s_i == "B" and (l_c - r_c) % 2 == 0:
            l_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 0:
            o_c = (o_c % mod) *  ((l_c - r_c) % mod)
            r_c += 1
        elif s_i == "B" and (l_c - r_c) % 2 == 1:
            o_c = (o_c % mod) * ((l_c - r_c) % mod)
            r_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 1:
            l_c += 1
        # print(s_i, l_c, r_c, o_c)
    # print(l_c, r_c, o_c)

    return ((o_c % mod) * (math.factorial(n) % mod)) % mod

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    s = list(sys.stdin.readline().strip())
    print(solve(n, s))

Submission status: Wrong Answer

Solution code:
import sys
import math
from pprint import pprint

def solve(n, s):

    ans = 0
    if s[0] == "W" or s[2*n-1] == "W":
        return ans

    l_c = 0
    r_c = 0
    o_c = 1
    mod = 10**9 + 7
    for s_i in s:
        if s_i == "B" and (l_c - r_c) % 2 == 0:
            l_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 0:
            o_c = (o_c % mod) *  ((l_c - r_c) % mod)
            r_c += 1
        elif s_i == "B" and (l_c - r_c) % 2 == 1:
            o_c = (o_c % mod) * ((l_c - r_c) % mod)
            r_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 1:
            l_c += 1
        # print(s_i, l_c, r_c, o_c)
    # print(l_c, r_c, o_c)

    return (o_c * math.factorial(n)) % mod

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    s = list(sys.stdin.readline().strip())
    print(solve(n, s))

Submission status: Wrong Answer

Solution code:
import sys
import math
from pprint import pprint

def solve(n, s):

    ans = 0
    if s[0] == "W" or s[2*n-1] == "W":
        return ans

    l_c = 0
    r_c = 0
    o_c = 1
    for s_i in s:
        if s_i == "B" and (l_c - r_c) % 2 == 0:
            l_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 0:
            o_c *= (l_c - r_c)
            r_c += 1
        elif s_i == "B" and (l_c - r_c) % 2 == 1:
            o_c *= (l_c - r_c)
            r_c += 1
        elif s_i == "W" and (l_c - r_c) % 2 == 1:
            l_c += 1
        # print(s_i, l_c, r_c, o_c)
    # print(l_c, r_c, o_c)

    return o_c * math.factorial(n)

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    s = list(sys.stdin.readline().strip())
    print(solve(n, s))

Submission status: Wrong Answer

Solution code:
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)
print(np.array(values).prod()*math.factorial(n))

Submission status: Wrong Answer

Solution code:
from math import*
M=10**9+7
n=int(input())
s=input()
if s[0]==s[-1]=='W':
  print(0)
  exit()
l=a=1
r=f=0
for b,c in zip(s,s[1:]):
  if b==c:
    f^=1
  if f:
    a*=r-l
    a%=M
    r+=1
  else:
    l+=1
print(a*factorial(n)%M*(l==r))

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7
N = int(input())
S = input()
 
if S[0] == "W" or S[-1] == "W":
  print(0)
  exit()

counter = 0
output = 1
 
for i in range(1, N+1):
  output *= i
  output %= MOD
  
for i, s in enumerate(S[::-1]):
  k = 2 * N - i - 1
  if (s == "W" and k % 2 == 1) or (s =="B" and k % 2 == 0):
    output *= (i - 2 * counter)
    output %= MOD
    counter += 1
print(output)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
MOD = 10**9 + 7
n = int(input())
s = input()
d = 1
ans = 1
for i in range(1, 2 * n):
    if (s[i] == 'B') ^ (d % 2 != 0):
        d += 1
    else:
        ans *= max(1, d)
        ans %= MOD
        d -= 1
if d != 0 or s[0] == 'W' or s[-1] == 'W':
    print(0)
    exit()
for i in range(1, n + 1):
    ans *= i
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
S = input()
mod = 10**9 + 7

left = 0
ret = 1
for i in range(len(S)):
    if S[i] == 'W':
        if left % 2:
            left += 1
        else:
            if left:
                ret *= left
                left -= 1
            else:
                print(0)
                exit()
    else:
        if left % 2:
            ret *= left
            left -= 1
        else:
            left += 1
                
for i in range(1, N+1):
    ret *= i
    ret %= mod
print(ret)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
MOD = 10**9 + 7
n = int(input())
s = input()
d = 1
ans = 1
for i in range(1, 2 * n):
    if (s[i] == 'B') ^ (d % 2 != 0):
        d += 1
    else:
        ans *= max(1, d)
        ans %= MOD
        d -= 1
if d != 0 or s[0] == 'W':
    print(0)
    exit()
for i in range(1, n + 1):
    ans *= i
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
S = input()
mod = 10**9 + 7

left = 0
ret = 1
for i in range(len(S)):
    if S[i] == 'W':
        if left % 2:
            left += 1
        else:
            if left:
                ret *= left
                left -= 1
            else:
                print(0)
                exit()
    else:
        if left % 2:
            ret *= left
            left -= 1
        else:
            left += 1
                
ret *= math.factorial(N)
ret %= mod
print(ret)

Submission status: Wrong Answer

Solution code:
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


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 = [1 for i in range(2*n)]

for i in range(len(LR_list)):
    li = LR_list[:i].count('L')
    ri = LR_list[:i].count('R')
    if LR_list[i] == 'R':
        values[i] = li-ri


print(np.array(values[1:]).prod()*math.factorial(n))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
MOD = 10**9 + 7
n = int(input())
s = input()
d = 1
ans = 1
for i in range(1, 2 * n):
    if (s[i] == 'B') ^ (d % 2 != 0):
        d += 1
    else:
        ans *= max(1, d)
        ans %= MOD
        d -= 1
if d != 0:
    print(0)
    exit()
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()
 
if S[0] == "W" or S[-1] == "W":
  print(0)
  exit()

counter = 0
output = 1

for i in range(1, N+1):
  output *= i % MOD

for i in range(2*N):
  k = 2 * N - i - 1
  if (S[k] == "W" and k % 2 == 1) or (S[k] =="B" and k % 2 == 0):
    output *= (i - 2 * counter)
    counter += 1
print(output % (10**9+7))

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    S = input()
    div = pow(10, 9) + 7
    facN = 1
    for i in range(1, N+1):
        facN = (facN * i) % div

    if S[0] != "B": return 0
    t = "L"
    for i in range(1, 2*N):
        if S[i-1] != S[i]:
            t += t[-1]
        else:
            if t[-1] == "L":
                t += "R"
            else:
                t += "L"
    if S[-1] != "B":
        return 0

    prefixL = [0]
    for c in t:
        if c == "L":
            prefixL.append(prefixL[-1]+1)
        else:
            prefixL.append(prefixL[-1])

    # print(S, t)
    # print(prefixL)
    ans = 1
    countR = 0
    for i in range(len(t)):
        if t[i] == "R":
            ans *= prefixL[i] - countR
            ans %= div
            countR += 1
    return (ans * facN) % div

if __name__ == '__main__':

    print(main())

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7

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

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

kai = [1 for _ in range(n+1)]
for i in range(1, n+1):
    kai[i] = kai[i-1] * i
    kai[i] %= MOD

def mul(a, b):
    return (a * b) % MOD

a = "L"
l = ["L", "R"]
c = 0

for i in range(1, len(s)):
    if s[i] == s[i-1]:
        c = not c
    a = a + l[c]


ans = 1
num = 0
for i in range(len(s)):
    if a[i] == "L":
        num += 1
    else:
        ans *= num
        ans %= MOD
        num -= 1

ans *= kai[n]
ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    S = input()
    div = pow(10, 9) + 7
    facN = 1
    for i in range(1, N+1):
        facN = (facN * i) % div

    t = "L"
    for i in range(1, 2*N-1):
        if S[i-1] != S[i]:
            t += t[-1]
        else:
            if t[-1] == "L":
                t += "R"
            else:
                t += "L"
    t += "R"

    prefixL = [0]
    for c in t:
        if c == "L":
            prefixL.append(prefixL[-1]+1)
        else:
            prefixL.append(prefixL[-1])

    # print(S, t)
    # print(prefixL)
    ans = 1
    countR = 0
    for i in range(len(t)):
        if t[i] == "R":
            ans *= prefixL[i] - countR
            ans %= div
            countR += 1
    return (ans * facN) % div

if __name__ == '__main__':

    print(main())

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
ls=[]
for i in range(2*N):
  if S[i]=='B':
    f=1
  else:
    f=0
  ls.append((f+i)%2)
count_sub=0
count_reverse=0
ans=1
for i in range(2*N):
  if ls[i]==1:
    count_sub+=1
  else:
    ans=(ans*count_sub)%1000000007
    count_sub-=1
for i in range(1,N+1):
  ans=(ans*i)%1000000007
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6 + 1000)

words = lambda t : list(map(t, input().split()))
n = int(input())
s = input()

a = [0] * len(s)
for i in range(len(s)):
    if s[i] == "B":
        a[i] = 1
    else:
        a[i] = 0

l = 0
r = len(a)-1

def recur(l,r,cur_n,k):
    #print(l,r,cur_n,k)
    if l >= r:
        return 0
    if l+1 == r:
        if a[l] == a[r]:
            return 1
        else:
            return 0
    ret = 0
    if a[l] == a[l+1] and a[l] == k:
        ret += recur(l+2,r, cur_n-1,k)
        ret %= (10**9 + 7)
    if a[r] == a[r-1] and a[r] == k:
        ret += recur(l,r-2,cur_n-1,k)
        ret %= (10**9 + 7)
    if a[l] == a[r] and a[l]== k:
        ret += recur(l+1,r-1,cur_n-1,(k+1)%2)
        ret %= (10**9 + 7)
    #print(ret)
    return cur_n * (cur_n - 1) * ret % (10**9 + 7)

print(recur(l,r,n,1))

Submission status: Wrong Answer

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

if s[0] == "W" or s[-1] == "W":
    print(-1)
    exit()
front = 0
ans = 1
for i, item in enumerate(s):
    if item == "B":
        if front % 2 == 0:
            front += 1
        else:
            ans *= front
            ans %= MOD
            front -= 1
    else:
        if front % 2 == 1:
            front += 1
        else:
            ans *= front
            ans %= MOD
            front -= 1
    if front < 0:
        print(-1)
        exit()
if front != 0:
    print(-1)
    exit()
for i in range(1, n+1):
    ans *= i
    ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

if s[0] == 'W' or s[-1] == 'W':
    print(0)
else:    
    s_lr = 'L'
    for i in range(len(s)-1):
        if s[i+1] == s[i]:
            if s_lr[-1] == 'L':
                s_lr += 'R'
            else:
                s_lr += 'L'
        else:
            if s_lr[-1] == 'L':
                s_lr += 'L'
            else:
                s_lr += 'R'    
    l_cnt = 0
    r_cnt = 0
    lst = []
    for i in s_lr:
        if i == 'L':
            l_cnt += 1
            lst.append(0)
        else:
            if l_cnt - r_cnt <= 0:
                print(0)
                break
            else:
                lst.append(l_cnt - r_cnt)
            r_cnt += 1
    else:
        if l_cnt != r_cnt:
            print(0)
        else:
            res = 1
            for i in lst:
                if i > 0:
                    res = res % (10**9 + 7)
            print(res * math.factorial(n) % (10**9 + 7) )

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7

n = int(input())
S = input()
ss = [1 if s == "W" else 0 for s in S]

kai = [1 for _ in range(n+1)]
for i in range(1, n+1):
    kai[i] = kai[i-1] * i
    kai[i] %= MOD

def mul(a, b):
    return (a * b) % MOD


ans = 1
num = 0
for s in ss:
    if num % 2 == s:
        num += 1
    else:
        ans *= num
        ans %= MOD
        num -= 1

ans *= kai[n]
ans %= MOD

print(ans)