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:
import sys
MOD=10**9+7
N=int(input())
S=list(input())

if S[0]=="W" or S[-1]=="W":
  print(0)
  sys.exit(0)
elif S.count("B")!=S.count("W"):
  print(0)
  sys.exit(0)

answer=1
for i in range(2,N+1):
  answer*=i
  answer%=MOD
  
lrlist=["L"]
for i in range(1,2*N):
  if S[i]==S[i-1]:
    if lrlist[-1]=="L":
      lrlist.append("R")
    else:
      lrlist.append("L")
  else:
    if lrlist[-1]=="L":
      lrlist.append("L")
    else:
      lrlist.append("R")
#print(lrlist)
  
num_L=num_R=0
for i in range(2*N):
  x=lrlist[i]
  if x=="R":
    #print(num_L,num_R,num_L-num_R)
    answer*=num_L-num_R
    answer%=MOD    
  
  if x=="L":
    num_L+=1
  else:
    num_R+=1
    
print(answer)

Submission status: Wrong Answer

Solution code:
import sys

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

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

d = {'L':'R', 'R':'L'}

T = 'L'
for i in range(2 * N-1):
    if S[i] == S[i+1]:
        T += d[T[-1]]
    else:
        T += T[-1]

if T.count('L') != N:
    print(0)
    sys.exit()

ans = 1
cnt = 0

for i in range(2 * N):
    if T[i] == 'L':
        cnt += 1
    else:
        ans *= cnt
        cnt -= 1

def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact

ans *= factorial(N)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
s=input()
MOD=10**9+7
if s[0]=='W' or s[-1]=='W':
  print(0)
  sys.exit()
a=[-1]*(2*n)
current=1
for i in range(2*n-1):
  a[i]=current
  if s[i]==s[i+1]:
    current=1-current
if current==1:
  print(0)
  sys.exit()
a[2*n-1]=0
if sum(a)!=n:
  print(0)
  sys.exit()
ans=1
current=0
for i in range(2*n):
  if a[i]==1:
    current+=1
  else:
    current-=1
  if current<0:
    print(0)
    sys.exit()
current=0
for i in range(2*n):
  if a[2*n-1-i]==0:
    current+=1
  else:
    current-=1
  if current<0:
    print(0)
    sys.exit()
current=0
for i in range(2*n):
  if a[2*n-1-i]==1:
    current+=1
  else:
    current-=1
    ans=(ans*current)%MOD
for i in range(1,n+1):
  ans=(ans*i)%MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
s=input()
MOD=10**9+7
if s[0]=='W' or s[-1]=='W':
  print(0)
  sys.exit()
a=[-1]*(2*n)
current=1
for i in range(2*n-1):
  a[i]=current
  if s[i]==s[i+1]:
    current=1-current
if current==1:
  print(0)
  sys.exit()
a[2*n-1]=0
if sum(a)!=n:
  print(0)
  sys.exit()
ans=1
current=0
for i in range(2*n):
  if a[2*n-1-i]==1:
    current+=1
  else:
    current-=1
    ans=(ans*current)%MOD
for i in range(1,n+1):
  ans=(ans*i)%MOD
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
cnt=0
r=[""]*(2*n)
d={"B":1,"W":0}
for i in range(2*n):
    if d[s[i]]^cnt%2:
        cnt+=1
        r[i]="L"
    else:
        cnt-=1
        r[i]="R"
    if cnt<=-1 or cnt>=n+1:print(0);exit()
ans=1
mod=10**9+7
for i in range(1,n+1):
    ans*=i
    ans%=mod
lcnt=0
for i in range(2*n):
    if r[i]=="L":
        lcnt+=1
    if r[i]=="R":
        ans*=lcnt
        ans%=mod
        lcnt-=1
print(ans)

Submission status: Wrong Answer

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

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

sg = 1
ans = 1
now = 0
for i in range(1, n*2):
    if s[i] == s[i-1]:
        now = (now+1)%2
    if now == 0:
        sg += 1
    else:
        ans = (ans * sg) % mod
        sg -= 1
    if sg < 0 or sg > n:
        ans = 0
        break
    # print(i,sg)

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import *

N = int(input())
S = input()[:-1]

if S[0]=='W':
    print(0)
    exit()

l = [0]
ans = 1
MOD = 10**9+7

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

zero = 0

for li in l:
    if li==0:
        zero += 1
    else:
        ans *= zero
        ans %= MOD
        zero -= 1
        
for i in range(1, N+1):
    ans *= i
    ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from math import factorial

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline
MOD = 10 ** 9 + 7


def solve():
    N = int(rl())
    S = rl().rstrip()
    
    if S[0] == 'W' or S[-1] == 'W':
        print(0)
        return
    
    flip = [0] * (2 * N)
    cntl, cntr = 1, 0
    ans = 1
    for i in range(1, 2 * N):
        if S[i] == S[i - 1]:
            flip[i] = flip[i - 1] ^ 1
        else:
            flip[i] = flip[i - 1]
        if flip[i] == 0:
            cntl += 1
        else:
            ans = ans * (cntl - cntr) % MOD
            cntr += 1
    
    if cntl != cntr:
        print(0)
    else:
        ans = ans * factorial(N)
        print(ans)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import heapq

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

fact=[i for i in range(2*N+1)]
fact[0]=1
for i in range(1,2*N+1):
    fact[i]*=fact[i-1]
    fact[i]%=mod

inverse=[pow(fact[i],mod-2,mod) for i in range(2*N+1)]

data=["" for i in range(2*N)]
for i in range(2*N):
    if S[i]=="B" and i%2==1:
        data[i]="L"
    elif S[i]=="B" and i%2==0:
        data[i]="R"
    elif S[i]=="W" and i%2==1:
        data[i]="R"
    elif S[i]=="W" and i%2==0:
        data[i]="L"


if data[0]=="L" or data[-1]=="R":
    print(0)
    exit()

ans=fact[N]
data=groupby(data)
que=[]
heapq.heapify(que)
i=0
for key,group in data:
    g=len(list(group))
    heapq.heappush(que,(i,g))
    i+=1

rest=0
while que:
    i,R=heapq.heappop(que)
    j,L=heapq.heappop(que)
    if L>R+rest:
        print(0)
        break
    else:
        ans*=fact[R+rest]*inverse[R+rest-L]
        ans%=mod
        rest=R+rest-L
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7

def cmb(n, k):
    if k < 0 or k > n: return 0 
    return fact[n] * fact_inv[k] % MOD * fact_inv[n-k] % MOD

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 10 ** 6  # 階乗テーブルの上限
fact, fact_inv = make_fact(U, MOD)

# 最終形が白一色、逆再生、leftとrightで別に考える
N = ir()
S = sr()
cur = 'W'  # 左端に白があるとする
left = 0
right = 0
dic = {'W': 'B', 'B': 'W'}
answer = fact[N]
X = []
for s in S:
    if s == cur:
        right += 1
        cur = dic[s]
    else:
        left += 1
        remain = N - right
        X.append(remain)
        cur = s
    if right > left:
        print(0); exit()

if right != left:
    print(0); exit()
    
for i, x in enumerate(X[::-1]):
    answer *= (x - i)

print(answer % MOD)
# 07

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7

def cmb(n, k):
    if k < 0 or k > n: return 0 
    return fact[n] * fact_inv[k] % MOD * fact_inv[n-k] % MOD

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 10 ** 6  # 階乗テーブルの上限
fact, fact_inv = make_fact(U, MOD)

# 最終形が白一色、逆再生、leftとrightで別に考える
N = ir()
S = sr()
cur = 'W'  # 左端に白があるとする
left = 0
right = 0
dic = {'W': 'B', 'B': 'W'}
answer = fact[N]
X = []
for s in S:
    if s == cur:
        right += 1
        cur = dic[s]
    else:
        left += 1
        remain = N - right
        X.append(remain)
        cur = s
    if right > left:
        print(0); exit()

for i, x in enumerate(X[::-1]):
    answer *= (x - i)

print(answer % MOD)
# 07

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7

def cmb(n, k):
    if k < 0 or k > n: return 0 
    return fact[n] * fact_inv[k] % MOD * fact_inv[n-k] % MOD

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 10 ** 6  # 階乗テーブルの上限
fact, fact_inv = make_fact(U, MOD)

# 最終形が白一色、逆再生、leftとrightで別に考える
N = ir()
S = sr()
cur = 'W'  # 左端に白があるとする
left = 0
right = 0
dic = {'W': 'B', 'B': 'W'}
answer = fact[N] ** 2
for s in S:
    if s == cur:
        right += 1
        cur = dic[s]
    else:
        left += 1
        remain = N - right
        answer *= remain
        answer //= N
        cur = s
    if right > left:
        print(0); exit()
    
print(answer % MOD)
# 07

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

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

kaijyo = [1]
for i in range(n):
	x = (kaijyo[i] * (i+1)) % MOD
	kaijyo.append(x)


if (s[0] == 'B') and (s[-1] == 'B'):
	string = []
	cnt = 1
	for i in range(2*n-1):
		if s[i] == s[i+1]:
			cnt += 1
		else:
			string.append(cnt)
			cnt = 1
		if i == 2*n-2:
			string.append(cnt)
	m = (len(string)-1)//2
	#print(string)
	if len(string) % 2 == 0:
		print(0)
	elif string[m] % 2 == 1:
		print(0)
	else:
		l = []
		ng = False
		for i in range(m):
			if string[i] % 2 == 0:
				ng = True
				break
			elif string[i] != 1:
				k = (string[i] - 1)//2
				for _ in range(k):
					l.append(i+1)
		#print('前半', l)
		if string[m]//2 != 1:
			for i in range(string[m]//2-1):
				l.append(m+1)
		#print('真ん中', l)
		for j in range(m+1, len(string)):
			if string[j] % 2 == 0:
				ng = True
				break
			elif string[j] != 1:
				for _ in range((string[j] - 1)//2):
					l.append(len(string)-j)
		#print('後半', l)
		if ng:
			print(0)
		else:
			ans = (kaijyo[n] * kaijyo[m+1]) % MOD
			for i in range(len(l)):
				ans = (ans * l[i]) % MOD
			print(ans)

else:
	print(0)

Submission status: Wrong Answer

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

kaijyo = [1]
for i in range(n):
	x = (kaijyo[i] * (i+1)) % MOD
	kaijyo.append(x)


if (s[0] == 'B') and (s[-1] == 'B'):
	string = []
	cnt = 1
	for i in range(2*n-1):
		if s[i] == s[i+1]:
			cnt += 1
		else:
			string.append(cnt)
			cnt = 1
		if i == 2*n-2:
			string.append(cnt)
	m = (len(string)-1)//2
	if len(string) % 2 == 0:
		print(0)
	elif string[m] % 2 == 1:
		print(0)
	else:
		l = []
		for i in range(m):
			if string[i] != 1:
				k = (string[i] - 1)//2
				for _ in range(k):
					l.append(i+1)
		for j in range(m+1, len(string)):
			if string[j] != 1:
				k = (string[j] - 1)//2
				for _ in range(k):
					l.append(len(string)-j)
		ans = (kaijyo[n] * kaijyo[m+1]) % MOD
		for i in range(len(l)):
			ans = (ans * l[i]) % MOD
		print(ans)

				
else:
	print(0)

Submission status: Wrong Answer

Solution code:
N,S = open(0).read().split()
N = int(N)
T = []
MOD = 10**9+7
for i,c in enumerate(S):
  if i%2==0:
    if c=='B':
      T.append('W')
    else:
      T.append('B')
  else:
    T.append(c)

pre = 0
ans = 1
for c in T:
  if c=='B':
    ans *= pre
    ans %= MOD
    pre -= 1
  else:
    pre += 1
coef = 1
for i in range(1,N+1):
  coef *= i
  coef %= MOD
print(ans*coef%MOD)

Submission status: Wrong Answer

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

count=0
ans=1
for i in range(2*n):
    if s[i]=="B": p=1
    else: p=0
    if (count+p)%2==0:
        ans=ans*count%mod
        count-=1
    else: count+=1
    if count<0: print(0);sys.exit()

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

Submission status: Wrong Answer

Solution code:
import sys
N=int(input())
S=list(input())
mod=10**9+7
data=[0]*2*N
if S[0]=="W":
    print(0)
    sys.exit()
data[0]=-1
for i in range(1,2*N):
    if S[i]==S[i-1]:
        data[i]=-data[i-1]
    else:
        data[i]=data[i-1]
if data[-1]==-1:
    print(0)
    sys.exit()
lst=[0]*2*N
lst[-1]=data[-1]
for i in range(2*N-2,-1,-1):
    lst[i]=lst[i+1]+data[i]
ans=1
for i in range(2*N-1,-1,-1):
    if data[i]==-1:
        ans=ans*(lst[i]+1)%mod

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

Submission status: Wrong Answer

Solution code:
import sys
N=int(input())
S=list(input())
mod=10**9+7
data=[0]*2*N
if S[0]=="W":
    print(0)
    sys.exit()
data[0]=-1
for i in range(1,2*N):
    if S[i]==S[i-1]:
        data[i]=-data[i-1]
    else:
        data[i]=data[i-1]
if data[-1]==-1:
    print(-1)
    sys.exit()
lst=[0]*2*N
lst[-1]=data[-1]
for i in range(2*N-2,-1,-1):
    lst[i]=lst[i+1]+data[i]
ans=1
for i in range(2*N-1,-1,-1):
    if data[i]==-1:
        ans=ans*(lst[i]+1)%mod

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

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
m=10**9+7
l=1
for i in range(1,n+1):l=(l*i)%m
t=[0]*(2*n)
t[0]=1
for i in range(1,2*n):
    if s[i-1]==s[i]:
        t[i]=1-t[i-1]
    else:
        t[i]=t[i-1]
if t.count(1)==n:
    p=1
    c=0
    for i in t:
        c+=i
        if i==0:
            p=(p*c)%m
            c-=1
    print((p*l)%m)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    S=input()
    A=[((S[i]=='B') is (i&1==1))+0 for i in range(2*n)]

    cnt=0
    ans=1
    for a in A:
        if(a==0):
            cnt+=1
        else:
            ans*=cnt
            ans%=MOD
            cnt-=1

    # factorial(n) を掛ける
    for i in range(1,n+1):
        ans*=i
        ans%=MOD
    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()

class modfact(object):
    def __init__(self,n):
        fact=[1]*(n+1); invfact=[1]*(n+1)
        for i in range(1,n+1): fact[i]=i*fact[i-1]%MOD
        invfact[n]=pow(fact[n],MOD-2,MOD)
        for i in range(n-1,-1,-1): invfact[i]=invfact[i+1]*(i+1)%MOD
        self.__fact=fact; self.__invfact=invfact

    def inv(self,n):
        assert(n>0)
        return self.__fact[n-1]*self.__invfact[n]%MOD

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

    def invfact(self,n):
        return self.__invfact[n]

    def comb(self,n,k):
        if(k<0 or n<k): return 0
        return self.__fact[n]*self.__invfact[k]*self.__invfact[n-k]%MOD

    def perm(self,n,k):
        if(k<0 or n<k): return 0
        return self.__fact[n]*self.__invfact[n-k]%MOD

def resolve():
    n=int(input())
    mf=modfact(n)
    S=input()

    A=[None]*(2*n)
    for i,s in enumerate(S,1):
        A[i-1]=(s=='W')^(i&1)
    B=A[:]
    for i in range(2*n-1):
        B[i+1]+=B[i]

    # A が 0 であるものに対して、自分より左に何個 1 があるかを考える
    if(A[0]==0 or A[1]==0):
        print(0)
        return

    now=0
    ans=1
    for i in range(2*n):
        if(A[i]==0):
            ans*=B[i-1]-now
            ans%=MOD
            now+=1

    print(ans*mf.fact(n)%MOD)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()

class modfact(object):
    def __init__(self,n):
        fact=[1]*(n+1); invfact=[1]*(n+1)
        for i in range(1,n+1): fact[i]=i*fact[i-1]%MOD
        invfact[n]=pow(fact[n],MOD-2,MOD)
        for i in range(n-1,-1,-1): invfact[i]=invfact[i+1]*(i+1)%MOD
        self.__fact=fact; self.__invfact=invfact

    def inv(self,n):
        assert(n>0)
        return self.__fact[n-1]*self.__invfact[n]%MOD

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

    def invfact(self,n):
        return self.__invfact[n]

    def comb(self,n,k):
        if(k<0 or n<k): return 0
        return self.__fact[n]*self.__invfact[k]*self.__invfact[n-k]%MOD

    def perm(self,n,k):
        if(k<0 or n<k): return 0
        return self.__fact[n]*self.__invfact[n-k]%MOD

def resolve():
    n=int(input())
    mf=modfact(n)
    S=input()

    A=[None]*(2*n)
    for i,s in enumerate(S,1):
        A[i-1]=(s=='W')^(i&1)
    B=A[:]
    for i in range(2*n-1):
        B[i+1]+=B[i]

    # A が 0 であるものに対して、自分より左に何個 1 があるかを考える
    if(A[0]==0 or A[1]==1):
        print(0)
        return

    now=0
    ans=1
    for i in range(2*n):
        if(A[i]==0):
            ans*=B[i-1]-now
            ans%=MOD
            now+=1

    print(ans*mf.fact(n)%MOD)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()

class modfact(object):
    def __init__(self,n):
        fact=[1]*(n+1); invfact=[1]*(n+1)
        for i in range(1,n+1): fact[i]=i*fact[i-1]%MOD
        invfact[n]=pow(fact[n],MOD-2,MOD)
        for i in range(n-1,-1,-1): invfact[i]=invfact[i+1]*(i+1)%MOD
        self.__fact=fact; self.__invfact=invfact

    def inv(self,n):
        assert(n>0)
        return self.__fact[n-1]*self.__invfact[n]%MOD

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

    def invfact(self,n):
        return self.__invfact[n]

    def comb(self,n,k):
        if(k<0 or n<k): return 0
        return self.__fact[n]*self.__invfact[k]*self.__invfact[n-k]%MOD

    def perm(self,n,k):
        if(k<0 or n<k): return 0
        return self.__fact[n]*self.__invfact[n-k]%MOD

def resolve():
    n=int(input())
    mf=modfact(n)
    S=input()

    A=[None]*(2*n)
    for i,s in enumerate(S,1):
        A[i-1]=(s=='W')^(i&1)
    B=A[:]
    for i in range(2*n-1):
        B[i+1]+=B[i]

    # A が 0 であるものに対して、自分より左に何個 1 があるかを考える
    if(A[0]==0):
        print(0)
        return

    now=0
    ans=1
    for i in range(2*n):
        if(A[i]==0):
            ans*=B[i-1]-now
            ans%=MOD
            now+=1

    print(ans*mf.fact(n)%MOD)
resolve()

Submission status: Wrong Answer

Solution code:
def main():
	mod=10**9+7
	n=int(input())
	a=input()+'z'
	if a[0]=='W':
		print(0)
		return 0
	x=[]
	cnt=1
	for i in range(2*n):
		if a[i]==a[i+1]:
			cnt+=1
		else:
			x.append(cnt)
			cnt=1
	lx=len(x)
	if lx==1:
		print(1)
		return 0
	if x[0]%2==0 or x[-1]%2==0:
		print(0)
		return 0
	y=[1]
	now=1
	cnt=1
	for i in range(lx-1):
		if x[i]%2!=0:
			cnt+=now
			y.append(cnt)
		else:
			now*=-1
			cnt+=now
			y.append(cnt)
	if y[-1]>1 or 0 in y:
		print(0)
		return 0
	#print(y)
	z=[1]+[y[i+1]-y[i] for i in range(lx-1)]
	ans=1
	for i in range(lx):
		if x[i]%2==0:
			ans=ans*pow(y[i],x[i]//2,mod)%mod
		else:
			ans=ans*pow(y[i],(x[i]-1)//2+(z[i]==-1),mod)%mod
	for i in range(2,n+1):
		ans=ans*i%mod
	print(ans)
main()

Submission status: Wrong Answer

Solution code:
def main():
	mod=10**9+7
	n=int(input())
	a=input()+'z'
	if a[0]=='W':
		print(0)
		return 0
	x=[]
	cnt=1
	for i in range(2*n):
		if a[i]==a[i+1]:
			cnt+=1
		else:
			x.append(cnt)
			cnt=1
	lx=len(x)
	#print(x)
	if x[0]%2==0 or x[-1]%2==0:
		print(0)
		return 0
	y=[1]
	now=1
	cnt=1
	for i in range(lx-1):
		if x[i]%2!=0:
			cnt+=now
			y.append(cnt)
		else:
			now*=-1
			cnt+=now
			y.append(cnt)
	if y[-1]>1 or 0 in y:
		print(0)
		return 0
	#print(y)
	z=[1]+[y[i+1]-y[i] for i in range(lx-1)]
	ans=1
	for i in range(lx):
		if x[i]%2==0:
			ans=ans*pow(y[i],x[i]//2,mod)%mod
		else:
			ans=ans*pow(y[i],(x[i]-1)//2+(z[i]==-1),mod)%mod
	for i in range(2,n+1):
		ans=ans*i%mod
	print(ans)
main()

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

Solution code:
def main():
	mod=10**9+7
	n=int(input())
	a=input()+'z'
	if a[0]=='W':
		print(0)
		return 0
	x=[]
	cnt=1
	for i in range(2*n):
		if a[i]==a[i+1]:
			cnt+=1
		else:
			x.append(cnt)
			cnt=1
	lx=len(x)
	if lx%2==0:
		print(0)
		return 0
	ans=1
	for i in range(lx//2):
		if x[i]%2==0:
			ans=0
		else:
			ans=ans*pow(i+1,x[i]//2,mod)%mod
	if x[lx//2]%2==1:
		ans=0
	else:
		ans=ans*pow(lx//2+1,x[lx//2]//2,mod)%mod
	for i in range(lx//2+1,lx):
		if x[i]%2==0:
			ans=0
		else:
			ans=ans*pow(lx-i,x[i]//2+1,mod)%mod
	for i in range(2,n+1):
		ans=ans*i%mod
	print(ans)
main()

Submission status: Wrong Answer

Solution code:
def main():
	mod=10**9+7
	n=int(input())
	a=input()+'z'
	if a[0]=='W':
		print(0)
		return 0
	x=[]
	cnt=1
	for i in range(2*n):
		if a[i]==a[i+1]:
			cnt+=1
		else:
			x.append(cnt)
			cnt=1
	lx=len(x)
	if lx%2==0:
		print(0)
		return 0
	ans=1
	for i in range(lx//2):
		if x[i]%2==0:
			ans=0
		else:
			ans=ans*pow(i+1,x[i]//2,mod)%mod
	if x[lx//2]%2==1:
		ans=0
	else:
		ans=ans*pow(lx//2+1,x[lx//2]//2,mod)%mod
	for i in range(lx//2+1,lx):
		if x[i]%2==0:
			ans=0
		else:
			ans=ans*pow(lx-i,x[i]//2+1,mod)
	for i in range(2,n+1):
		ans=ans*i%mod
	print(ans)
main()

Submission status: Wrong Answer

Solution code:
def main():
	mod=10**9+7
	n=int(input())
	a=input()+'z'
	if a[0]=='W':
		print(0)
		return 0
	x=[]
	cnt=1
	for i in range(2*n):
		if a[i]==a[i+1]:
			cnt+=1
		else:
			x.append(cnt)
			cnt=1
	lx=len(x)
	if lx%2==0:
		print(0)
		return 0
	ans=1
	for i in range(lx//2):
		if x[i]%2==0:
			ans=0
		else:
			ans*=((i+1)**(x[i]//2))
	if x[lx//2]%2==1:
		ans=0
	else:
		ans*=((lx//2+1)**(x[lx//2]//2))
	for i in range(lx//2+1,lx):
		if x[i]%2==0:
			ans=0
		else:
			ans=ans*pow(lx-i,x[i]//2+1,mod)
	for i in range(2,n+1):
		ans=ans*i%mod
	print(ans)
main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

ans = 1
R = [None]*(2*N)
for i in range(2*N):
    if S[i] == 'W':
        if i == 2*N-1:
            ans = 0
        if i % 2 == 1:
            R[i] = 1
        else:
            R[i] = 0
    else:
        if i % 2 == 0:
            R[i] = 1
        else:
            R[i] = 0

mod = 10**9+7
num_r = 0
for i in range(2*N):
    if R[i]:
        num_r += 1
    else:
        ans = (ans*num_r)%mod
        num_r -= 1
for n in range(1, N+1):
    ans = (ans*n)%mod
print(ans)

Submission status: Wrong Answer

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

R = [None]*(2*N)
for i in range(2*N):
    if S[i] == 'W':
        if i % 2 == 1:
            R[i] = 1
        else:
            R[i] = 0
    else:
        if i % 2 == 0:
            R[i] = 1
        else:
            R[i] = 0

mod = 10**9+7
num_r = 0
ans = 1
for i in range(2*N):
    if R[i]:
        num_r += 1
    else:
        ans = (ans*num_r)%mod
        num_r -= 1
for n in range(1, N+1):
    ans = (ans*n)%mod
print(ans)

Submission status: Wrong Answer

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

X = []
for i, s in enumerate(S):
    if i % 2 == 1:
        X.append(s)
    else:
        if s == 'W':
            X.append('B')
        else:
            X.append('W')


W_cnt = 0
ans = 1
for x in X:
    if x == 'W':
        W_cnt += 1
    else:
        ans *= W_cnt
        ans %= MOD
        W_cnt -= 1

print(ans * factorial(N) % MOD)

Submission status: Wrong Answer

Solution code:
import math

mod = 1000000007
N = int(input())
S = input()
cnt_L = 0
cnt = 1
newS = ''
if S.count('B') != N:
  cnt = 0
else:
  for i,s in enumerate(S):
    if i%2 == 0:
      s='W' if s=='B' else 'B'
    newS += s
    if s=='W':
      cnt_L += 1
    else:
      cnt = cnt * cnt_L
      cnt_L -= 1
print((cnt*math.factorial(N))%mod)

Submission status: Wrong Answer

Solution code:
import math

mod = 1000000007
N = int(input())
S = input()
cnt_L = 0
cnt = 1
newS = ''
if S.count('B') != N:
  cnt = 0
else:
  for i,s in enumerate(S):
    if i%2 == 0:
      s='W' if s=='B' else 'B'
    newS += s
    if s=='W':
      cnt_L += 1
    else:
      cnt = cnt * cnt_L
      cnt_L -= 1
print(cnt*math.factorial(N)%mod)

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
S = input()
cnt_L = 0
cnt = 1
newS = ''
if S.count('B') != N:
  cnt = 0
else:
  for i,s in enumerate(S):
    if i%2 == 0:
      s='W' if s=='B' else 'B'
    newS += s
    if s=='W':
      cnt_L += 1
    else:
      cnt = cnt * cnt_L
      cnt_L -= 1
print(cnt*math.factorial(N))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    s = input()
    divider = pow(10, 9) + 7
    if s[0] == "W" or s[2 * n - 1] == "W":
        print(0)
        return
    cnt = 1  # 左カッコの数
    lr = [True]
    for i in range(1, 2 * n - 1):
        if s[i - 1] == s[i]:
            cnt += 1
            lr.append(not lr[i - 1])
        else:
            lr.append(lr[i - 1])
    lr.append(False)
    if not cnt == n:
        print(0)
        return
    ret = 1
    cnt = 0
    for i in range(2 * n):
        if lr[i]:
            cnt += 1
        else:
            ret *= cnt
            ret %= divider
            cnt -= 1
    for i in range(n):
        ret *= i + 1
        ret %= divider
    print(ret)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    s = input()
    divider = pow(10, 9) + 7
    if s[0] == "w" or s[2 * n - 1] == "w":
        print(0)
        return
    cnt = 1  # 左カッコの数
    lr = [True]
    for i in range(1, 2 * n - 1):
        if s[i - 1] == s[i]:
            cnt += 1
            lr.append(not lr[i - 1])
        else:
            lr.append(lr[i - 1])
    lr.append(False)
    if not cnt == n:
        print(0)
        return
    ret = 1
    cnt = 0
    for i in range(2 * n):
        if lr[i]:
            cnt += 1
        else:
            ret *= cnt
            ret %= divider
            cnt -= 1
    for i in range(n):
        ret *= i + 1
        ret %= divider
    print(ret)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

S_fin = ''
for i, s in enumerate(S):
    if i % 2 == 1:
        S_fin += s
        continue

    if s == 'W':
        S_fin += 'B'
    else:
        S_fin += 'W'


ans = 1
W_cnt = 0
for s in S_fin:
    if s == 'W':
        W_cnt += 1
    else:
        ans *= W_cnt
        ans %= MOD

        W_cnt -= 1

print(ans * factorial(N) % MOD)

Submission status: Wrong Answer

Solution code:
from math import factorial
mod = 10 ** 9 + 7
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_swap * cnt_tmp) % mod
        cnt_tmp -= 1
print(cnt_swap * factorial(n))

Submission status: Wrong Answer

Solution code:
import sys, math
n = int(input())
s = list(input())
m = 10**9+7
store = 0
ans = 1

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 = ans % m * store
        store -= 1
    if store < 0:
        print(0)
        sys.exit()

print(ans*math.factorial(n) % m)

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') or arr[-1] == 'L':
    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())
m = 10**9+7
store = 0
ans = 1

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
    if store < 0:
        print(0)
        sys.exit()

print(ans*math.factorial(n) % m)

Submission status: Wrong Answer

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

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
    if store < 0:
        print(0)
        sys.exit()

print(ans*math.factorial(n))