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 itertools import accumulate
mod=1000000007
n=int(input())
S=input()
Flag=[0]*(2*n)
for i,s in enumerate(S):
    if i%2:
        Flag[i]=int(s=='W')
    else:
        Flag[i]=int(s=='B')
Acc=list(accumulate(Flag))
#Acc_rev=[0]*(2*n)
#Acc_rev[-1]=Flag[-1]^1
#for i in reversed(range(2*n-1)):
#    Acc_rev[i]=Acc_rev[i+1]+(Flag[i]^1)
ans=1
cnt=0
#print(Flag)
#print(Acc)
#print(Acc_rev)
for i,f in enumerate(Flag):
    if f:
        if i==2*n-1:
            ans=0
    else:
        tmp=Acc[i]-cnt
        ans*=tmp
        ans%=mod
        cnt+=1
for i in range(1,n+1):
    ans*=i
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
A = [i+1 for i in range(N)]
A = A + A[::-1]

ans = 1
for i,s in enumerate(S) :
    if i < N :
        if s == "B" :
            if i % 2 == 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i > 0 :
                ans *= A[i-1]
                ans %= 10**9+7
            else :
                ans = 0
        else :
            if i % 2 != 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i > 0 :
                ans *= A[i-1]
                ans %= 10**9+7
            else :
                ans = 0

    else :
        if s == "B" :
            if i % 2 != 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i < 2*N-1 :
                ans *= A[i+1]
                ans %= 10**9+7
            else :
                ans = 0
        else :
            if i % 2 == 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i < 2*N-1 :
                ans *= A[i+1]
                ans %= 10**9+7
            else :
                ans = 0

ans %= 10**9+7
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
L = list(S)
for i in range(0, 2*N, 2):
  L[i] = "B" if L[i] == "W" else "W"
S = "".join(L)
#print(S)
r = 1
i = 0
nw = 0
nb = 0
while i < 2*N:
  j = S.find("B", i)
  if j < 0:
    print(0)
    exit()
  k = S.find("W", j)
  if k < 0:
    k = 2*N
  #print(i, j, k)
  nw += j-i
  nb += k-j
  for i in range(min(nb, nw)):
    r = r*max(nw, nb)%1000000007
    nw -= 1
    nb -= 1
  i = k
if not(nw == 0 and nb == 0):
  print(0)
  exit()
for i in range(1, N+1):
  r = r*i%1000000007
print(r)

Submission status: Wrong Answer

Solution code:
from math import factorial
n = int(input())
s = input()
if (s[0] != 'B') | (s[-1] != 'B'): print(0); exit()
a = [1]+[0]*(2*n-1)
b = [0]*2*n
for i in range(1, 2*n):
    if s[i] == s[i-1]:
        a[i] = 1^a[i-1]
        b[i] = 1^b[i-1]
    else:
        a[i] = a[i-1]
        b[i] = b[i-1]
for i in range(1, 2*n):
    a[i] += a[i-1]-b[i]
c = 1
for i, j in zip(a, b[1:]):
    if j: c *= i*j
print(c*factorial(n))

Submission status: Wrong Answer

Solution code:
import  math
n = int(input())
s = input()
inv_cnt = []
for i in s:
    if i == "B":
        inv_cnt.append(1)
    else:
        inv_cnt.append(2)

ans = 1
key = 1
# print(inv_cnt)
yokuwakaranairisuto = [key]
for i in range(1, 2 * n):
    if inv_cnt[i] != inv_cnt[i - 1]:
        # print(i, inv_cnt[i])
        key += 1
    yokuwakaranairisuto.append(key)

yokuwakaranairisuto[-1] = 1
key = 1
for i in range(2 * n - 2, 0, -1):
    if inv_cnt[i] != inv_cnt[i + 1]:
        # print(i, inv_cnt[i])
        key += 1
    yokuwakaranairisuto[i] = min(yokuwakaranairisuto[i], key)
# print(yokuwakaranairisuto)

ans = 1
for i in range(1, 2 * n):
    if yokuwakaranairisuto[i] != yokuwakaranairisuto[i - 1]:
        ans *= yokuwakaranairisuto[i]
        ans %= 10 ** 9 + 7
if ans == 1:
    ans = 0
ans *= math.factorial(n)
ans %= 10 ** 9 + 7
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
ss = [s == 'W' for s in S]

def mul(a, b):
    return (a * b) % (10**9 + 7)

ans = 1
num = 0
for s in ss:
    if num % 2 == s:
        num += 1
    else:
        ans = mul(ans, num)
        num -= 1
        
for i in range(2, N + 1):
    ans = mul(ans, i)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

c = 0
pre_s = S[0]
for s in S[1:]:
    if pre_s != s:
        c += 1
        pre_s = s
    else:
        pass

if c != N:
    print(0)
else:
    print(math.factorial(N)*N*(N-1))

Submission status: Wrong Answer

Solution code:
#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
 
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline  #危険！基本オフにしろ！
 
def main():
    l=0
    n,a=open(0).read().split()
    n=int(n)
    
    l=a.find("B")
    r=a.rfind("B")
    if l==-1 or r==-1 or l==r:
        print(0)
        exit()
    a=a[l:r+1]
    wakeme=len([k for k,v in groupby(a)])-1
    if wakeme>len(a)//2:
        print(0)
        exit()
    
    c=1
    mod=10**9+7
    print((factorial(n)%mod)*(n-1)*( wakeme %mod)%mod)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import  math
n = int(input())
s = input()
inv_cnt = []
for i in s:
    if i == "B":
        inv_cnt.append(1)
    else:
        inv_cnt.append(2)

ans = 1
key = 1
# print(inv_cnt)
yokuwakaranairisuto = [key]
for i in range(1, 2 * n):
    if inv_cnt[i] != inv_cnt[i - 1]:
        # print(i, inv_cnt[i])
        key += 1
    yokuwakaranairisuto.append(key)

yokuwakaranairisuto[-1] = 1
key = 1
for i in range(2 * n - 2, 0, -1):
    if inv_cnt[i] != inv_cnt[i + 1]:
        # print(i, inv_cnt[i])
        key += 1
    yokuwakaranairisuto[i] = min(yokuwakaranairisuto[i], key)
# print(yokuwakaranairisuto)

ans = 1
for i in range(1, 2 * n):
    if yokuwakaranairisuto[i] != yokuwakaranairisuto[i - 1]:
        ans *= yokuwakaranairisuto[i]
if ans == 1:
    ans = 0
ans *= math.factorial(n)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
w = 0
l = 0
for i in range(len(S)):
    if S[i] == 'W':
        w += 1
    else:
        l += 1

inf = 10**9 + 7

a = 1
b = 1
for i in range(1, w+1):
    if i % 2 != 0:
        a *= i
        a %= inf
    else:
        a *= (i//2)
        a %= inf
for i in range(1, l+1):
    if i % 2 != 0:
        b *= i
        b %= inf
    else:
        b *= (i//2)
        b %= inf


ans = N * a * b * 2
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)

ans = 0
if S[0] == "W" or S[-1] == "W":
    ans = 0
else:
    C = Counter(S)
    if C["B"] == 2*N:
        ans = F.fact(N)

    elif C["B"] != C["W"]:
        ans = 0
    else:
        ans = 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
L = list(S)
for i in range(0, 2*N, 2):
  L[i] = "B" if L[i] == "W" else "W"
S = "".join(L)
#print(S)
r = 1
i = 0
nw = 0
nb = 0
while i < 2*N:
  j = S.find("B", i)
  if j < 0:
    print(0)
    exit()
  k = S.find("W", j)
  if k < 0:
    k = 2*N
  #print(i, j, k)
  nw += j-i
  nb += k-j
  for i in range(min(nb, nw)):
    r = r*max(nb, nw)%1000000007
    nw -= 1
    nb -= 1
  i = k
for i in range(1, N+1):
  r = r*i%1000000007
print(r)

Submission status: Wrong Answer

Solution code:
"""
N = int(input())
A, B = map(int,input().split())
lst = [list(map(int,input().split())) for i in range(M)]
A = list(map(int,input().split()))
"""

import math

mod = int(1e9+7)

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

""" 
##for test
N = 100000
S = 'B'*(2*N)
#print(S)
"""

rest = 0
cnt = 1
for i in range(2*N):
  if (i%2 == 0 and S[i]=='B') or (i%2 == 1 and S[i]=='W'): ## 続行
    rest += 1
  else: ## 枝を一つ繋げる
    if rest < 1: ## 繋げる枝がない場合
      print(0)
      exit()
    else:
      cnt = (cnt * rest) % mod
      rest -= 1
else:
  if rest != 0: ## 繋ぐことのできなかった枝が残っている場合
    print(0)
    exit()

ans = cnt * (math.factorial(N) % mod)
print(ans)

Submission status: Wrong Answer

Solution code:
from math import factorial
n = int(input())
s = [int(i == 'B') for i in input()]
if s[0]+s[-1] < 2: print(0); exit()
a = [1]+[0]*(2*n-1)
b = [0]*2*n
for i in range(1, 2*n):
    if s[i] == s[i-1]:
        a[i] = 1^a[i-1]
        b[i] = 1^b[i-1]
    else:
        a[i] = a[i-1]
        b[i] = b[i-1]
for i in range(1, 2*n):
    a[i] += a[i-1]-b[i]
c = 1
for i, j in zip(a, b[1:]):
    if j: c *= i*j
print(c*factorial(n))

Submission status: Wrong Answer

Solution code:
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

b = 0
w = 0
for s in S:
    if s == "B":
        b += 1
    else:
        w += 1

if b != w:
    print(0)
else:
    print(math.factorial(N)*2*combinations_count(N, 2))

Submission status: Wrong Answer

Solution code:
#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
 
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline  #危険！基本オフにしろ！
 
def main():
    l=0
    n,a=open(0).read().split()
    n=int(n)
    
    l=a.find("B")
    r=a.rfind("B")
    if l==-1 or r==-1 or l==r or l!=0 or r!=2*n-1:
        print(0)
        exit()
    
    c=1
    mod=10**9+7
    print((factorial(n)%mod)*(n-1)*((len([k for k,v in groupby(a)])-1)%mod)%mod)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
s=input()
count=0
now=s[0]
for i in s:
    if i!=now:
        count+=1
        now=i
ans=math.factorial(n) * math.factorial(s.count('W')) // math.factorial(count//2)
if count%2==1 or s[0]!='B' or s[-1]!='B':ans=0
print(ans%(10**9+7))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#import numpy as np
from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
 
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline  #危険！基本オフにしろ！
 
def main():
    l=0
    n,a=open(0).read().split()
    l=a.find("B")
    r=a.rfind("B")
    if l==-1 or r==-1 or l==r:
        print(0)
        exit()
    
    a=a[l:r+1]
    
    c=1
    n=int(n)
    print(factorial(n)*(n-1)*(len([k for k,v in groupby(a)])-1))
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
L = list(S)
for i in range(0, 2*N, 2):
  L[i] = "B" if L[i] == "W" else "W"
S = "".join(L)
#print(S)
r = 1
i = 0
nw = 0
nb = 0
while i < 2*N:
  j = S.find("B", i)
  if j < 0:
    print(0)
    exit()
  k = S.find("W", j)
  if k < 0:
    k = 2*N
  #print(i, j, k)
  nw += j-i
  nb += k-j
  for i in range(nb):
    r = r*nw%1000000007
    nw -= 1
    nb -= 1
  i = k
for i in range(1, N+1):
  r = r*i%1000000007
print(r)

Submission status: Wrong Answer

Solution code:
import math

n = int(input())
s = input()
if s.count("B") == n:
    print(math.factorial(n) * n * (n - 1) % (10 ** 9 + 7))
else:
    print(0)

Submission status: Wrong Answer

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

t = ["" for i in range(2*n)]

for i in range(2*n):
  if i%2==0 and s[i]=='B':
    t[i] = "R"
  elif i%2==0 and s[i]=='W':
    t[i] = "L"
  elif i%2==1 and s[i]=='B':
    t[i] = "L"
  elif i%2==1 and s[i]=='W':
    t[i] = "R"

cur = 1
rs, ls = 0, 0
for i in range(2*n-1, -1, -1):
  if t[i]=="L":
    ls += 1
  else:
    cur = (cur*(max(0, ls-rs)))%MOD
    rs += 1

MAX = 2*10**5+1
fact = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]
invfact = [0 for i in range(MAX)]

def comb_build(n):
  fact[0] = inv[0] = invfact[0] = 1
  fact[1] = inv[1] = invfact[1] = 1
  for i in range(2, n):
    fact[i] = fact[i-1]*i%MOD
    inv[i] = inv[MOD%i]*(MOD-MOD//i)%MOD
    invfact[i] = invfact[i-1]*inv[i]%MOD

comb_build(MAX)
print((cur*fact[n])%MOD)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-

import sys
import math
input = sys.stdin.readline

N = int(input())
s = input()

num_s = 0
num_w = 0
for i in range(N):
    if s[i] == 'S':
        num_s += 1
    elif s[i] == 'W':
        num_s += 1
if num_s != num_w:
    print(0)
    sys.exit()

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

print(ans % 1000000007)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
x=0
r=1
m=10**9+7
for s in S:
    if s=='W':
        if x%2==1:
            x+=1
        elif x>0:
            r=(r*x)%m
            x-=1
        else:
            r=-1
            break
    else:
        if x%2==0:
            x+=1
        else:
            r=(r*x)%m
            x-=1
if r==-1:
    print(0)
else:
    a=1
    for i in range(N):
        a=a*(i+1)%m
    print((r*a)%m)

Submission status: Wrong Answer

Solution code:
def kaijo(x):
	a=1
	for i in range(1,x+1):
		a*=i
		a%=10**9+7
	return a

n=int(input())
s=input()
b=0
for i in range(1,2*n):
	if s[i]!=s[i-1]:
		b+=1
b=(b-2)//2
w=0
for i in range(2*n):
	if s[i]=="W":
		w+=1

w//=2
mod=10**9+7
if b>=0:
	print(pow(3,b,mod)*pow(2,w,mod)*kaijo(n)%mod)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
x=0
r=1
m=10**9+7
for s in S:
    if s=='W':
        if x%2==1:
            x+=1
        elif x>0:
            r=(r*x)%m
            x-=1
        else:
            r=-1
            break
    else:
        if x%2==0:
            x+=1
        else:
            r=(r*x)%m
            x-=1
if r==-1:
    print(0)
else:
    a=1
    for i in range(N):
        a=a*(i+1)%m
    print(r*a)

Submission status: Wrong Answer

Solution code:
def kaijo(x):
	a=1
	for i in range(1,x+1):
		a*=i
		a%=10**9+7
	return a

n=int(input())
s=input()
b=0
for i in range(1,2*n):
	if s[i]!=s[i-1]:
		b+=1
b=(b-2)//2
w=0
for i in range(2*n):
	if s[i]=="W":
		w+=1

w//=2
mod=10**9+7
if b>=0:
	print(pow(3,b,mod)*pow(2,w,mod)*kaijo(n)%mod)
else:
	print(0)

Submission status: Wrong Answer

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

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

ans = 0
if S[0] == "W" or S[-1] == "W":
    ans = 0
else:
    C = Counter(S)
    if C["B"] != C["W"]:
        ans = 0
    else:
        ans = 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(1000000)


def getN():
    return int(input())


def getList():
    return list(map(int, input().split()))


import math

def factorial(n):
    ans = 1
    for i in range(1, n+1):
        ans *= i

    return ans

n = getN()
s = input().strip()

ans = 1
cur = 0
open = 0
for c in s:
    # print(c, ans, open, cur)
    if c == "B":
        if open%2 == 0:
            open += 1
            cur = 1
        else:
            if open == 0:
                print(0)
                sys.exit()
            ans *= open
            open -= 1
            cur = 0

    else:
        if open%2 == 1:
            open += 1
            cur = 0
        else:
            if open == 0:
                print(0)
                sys.exit()
            ans *= open
            open -= 1
            cur = 0

if open != 0:
    print(0)
    sys.exit()
print(ans * factorial(n))

Submission status: Wrong Answer

Solution code:
"""
N = int(input())
A, B = map(int,input().split())
lst = [list(map(int,input().split())) for i in range(M)]
A = list(map(int,input().split()))
"""

import math

mod = int(1e9+7)

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

rest = 0
cnt = 1
for i in range(2*N):
  if (i%2 == 0 and S[i]=='B') or (i%2 == 1 and S[i]=='W'): ## 続行
    rest += 1
  else: ## 枝を一つ繋げる
    if rest < 1: ## 繋げる枝がない場合
      print(0)
      exit()
    else:
      cnt *= rest
      rest -= 1

ans = cnt * math.factorial(N) % mod
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

def fact(n):
    ans = 1
    for n in range(n):
        ans *= (n + 1)
    return ans


def solve(N, S):
    n = 0
    ans = 0
    local_ans = 0
    for ch in S:
        if n == 0:
            if ch == 'W':
                return 0
            else:
                n += 1
                local_ans = 1
        elif n % 2 == 1:
            if ch == 'W':
                n += 1
            else:
                local_ans *= n
                n -= 1
        else:
            if ch == 'W':
                local_ans *= n
                n -= 1
            else:
                n += 1
                
        if n == 0:
            ans += local_ans

        # print("ch, n, ans, N = ", ch, n, ans, N, fact(N))

    if n != 0:
        return 0

    return ans * fact(N)


print(solve(N, S))

# assert(solve(1, "BB") == 1)
# assert(solve(1, "WW") == 0)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline

N, = map(int, input().split())
L = list(map(lambda x: 1 if x == "B" else 0, input().strip()))

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

print(*L)

# 1 0 1 1 0 0 0 1

Submission status: Wrong Answer

Solution code:
def solve():
    MOD = 10**9 + 7

    N = int(input())
    Ss = input()

    N2 = 2*N
    LRs = [0] * N2
    numL = 0
    for i, S in enumerate(Ss):
        if numL == 0:
            if S == 'W':
                return 0
            LRs[i] = 'L'
            numL += 1
        else:
            if S == 'W':
                if numL % 2:
                    LRs[i] = 'L'
                    numL += 1
                else:
                    LRs[i] = 'R'
                    numL -= 1
            else:
                if numL % 2:
                    LRs[i] = 'R'
                    numL -= 1
                else:
                    LRs[i] = 'L'
                    numL += 1

    ans = 1
    numL = 0
    for LR in LRs:
        if LR == 'L':
            numL += 1
        else:
            if numL <= 0:
                return 0
            ans *= numL
            ans %= MOD
            numL -= 1

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

    return ans*fact%MOD


ans = solve()
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(1000000)


def getN():
    return int(input())


def getList():
    return list(map(int, input().split()))


import math

def factorial(n):
    ans = 1
    for i in range(1, n+1):
        ans *= i

    return ans

n = getN()
s = input().strip()

ans = 1
cur = 0
open = 0
for c in s:
    # print(c, ans, open, cur)
    if c == "B":
        if open%2 == 0:
            open += 1
            cur = 1
        else:
            if open == 0:
                print(0)
                sys.exit()
            ans *= open
            open -= 1
            cur = 0

    else:
        if open%2 == 1:
            open += 1
            cur = 0
        else:
            if open == 0:
                print(0)
                sys.exit()
            ans *= open
            open -= 1
            cur = 0


print(ans * factorial(n))

Submission status: Wrong Answer

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

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

    elif s[i] == 'W':
        if i % 2 == 0:
            r.append(i)
        else:
            l.append(i)


from bisect import bisect_left
ans = 1
counter = 0
for i in reversed(range(len(l))):
    _l = l[i]
    idx = bisect_left(r, _l)

    ans *= max((len(r) - counter - idx), 0)
    ans = ans % (10 ** 9 + 7)
    counter += 1

    # print(idx + counter)
from math import factorial

ans *= factorial(len(r))
print(ans % (10 ** 9 + 7))

Submission status: Wrong Answer

Solution code:
import sys, math
from collections import deque

readline = sys.stdin.readline

N = int(readline())
S = readline().strip()

P = 10 ** 9 + 7

d = 0
count = 1
for c in S:
    if d % 2 == 0:
        if c == 'B':
            d += 1
        else:
            count *= d
            count %= P
            d -= 1
    else:
        if c == 'B':
            count *= d
            count %= P
            d -= 1

        else:
            d += 1
    if d < 0:
        print(0)
        exit(0)

ans = count
for i in range(1, N + 1):
    ans *= i
    ans %= P

print(ans)

Submission status: Wrong Answer

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

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

for i in range(2*N):
    if S[i]=="B":
        S[i]=0
    else:
        S[i]=1

    if i%2==1:
        S[i]=(S[i]+1)%2


ones=0
ANS=1

for i in range(2*N-1,-1,-1):
    if S[i]==1:
        ones+=1

    else:
        ANS=ANS*ones%mod
        ones-=1


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


print(ANS)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7

def frac(limit):
    frac = [1]*limit
    for i in range(2,limit):
        frac[i] = i * frac[i-1]%mod
    fraci = [None]*limit
    fraci[-1] = pow(frac[-1], mod -2, mod)
    for i in range(-2, -limit-1, -1):
        fraci[i] = fraci[i+1] * (limit + i + 1) % mod
    return frac, fraci
frac, fraci = frac(200000)

def count(S):
    res = 1
    cnt = 0
    for s in S:
        if s:
            cnt += 1
        else:
            res = (res*cnt)%mod
            cnt -= 1
    
    return res
N = int(input())
S = [1 if s == 'B' else 0 for s in input().strip()]
L = 2*N
S = [(S[i] + i)%2 for i in range(L)]
print(count(S)*frac[N]%mod)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7
N = int(input())
S = [0 if s == 'W' else 'B' for s in list(input())]
def fact(n):
	ret = 1
	while n > 0:
		ret = (ret * n) % MOD
		n -= 1
	return ret

print(0)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7
N = int(input())
S = [0 if s == 'W' else 'B' for s in list(input())]
def fact(n):
	ret = 1
	while n > 0:
		ret = (ret * n) % MOD
		n -= 1
	return ret

print((N*(N-1)*fact(N)%MOD))

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] == "W": return 0

    count = 1
    for i in range(2*N):
        if S[i] == "B":
            count *= (i+2) // 2
        else:
            count *= (i+2) // 2

    return count // 2


if __name__ == '__main__':

    print(main())

Submission status: Wrong Answer

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

m = 1

for i in range(1,n+1):
    m*=i
    m%=10**9+7

if s[0]=='B' and s[-1]=='B':
    if s.count('B')%2==0:
        l = [1]
        for i in range(1,len(s)):
            if s[i-1]!=s[i] and l[-1]==1:
                l.append(1)
            elif s[i-1]==s[i] and l[-1]==0:
                l.append(1)
            else:
                l.append(0)
        x = [1]
        for i in range(1,len(l)):
            if l[i]==1:
                x.append(x[-1]+1)
            else:
                x.append(x[-1]-1)
        ans = 1
        for i in range(len(l)):
            if l[i]==0:
                ans *= x[i-1]
                ans %= 10**9+7
        print(m*ans%(10**9+7))
        exit()
print(0)

Submission status: Wrong Answer

Solution code:
import sys
mod = 10**9 + 7
N=int(input())
S = input()
leftL = 0
leftR = 0
ISR = []
for s in S:
  if s=="B":#leftL-leftRと同符号
    isR = abs(leftL-leftR)
  else:
    isR = int(not bool(abs(leftL-leftR)))
  if isR:
    leftR = (leftR+1)%2
  else:
    leftL = (leftL+1)%2
  ISR.append(isR)

zanki = 0
ans = 1
for i in ISR:
  if zanki<0:
    print(0)
    sys.exit()
  if i:#Rの場合
    ans *= zanki
    zanki -= 1
  else:
    zanki += 1
  ans %= mod

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

print(ans)

Submission status: Wrong Answer

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

ans = 1
mul = 0
for i in range(n * 2):
    if s[i] == "B":
        if mul % 2 == 0:
            mul += 1
        else:
            ans *= mul
            ans %= mod
            mul -= 1
    else:
        if mul % 2 == 1:
            mul += 1
        else:
            ans *= mul
            ans %= mod
            mul -= 1
for i in range(n):
    ans *= (i + 1)
    ans %= mod
print(ans)

Submission status: Wrong Answer

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

def get_num(s, iii):
    if s=="B":
        for i in range(iii,-1,-1):
            if i%2==1:
                return i
    else:
        for i in range(iii,-1,-1):
            if i%2==0:
                return i

ans=1
for i in range(n):
    left=s[i]
    right=s[-(i+1)]
    ans*=get_num(left, i+1)
    ans%=mod
    ans*=get_num(right, i+1)
    ans%=mod

print(ans)

Submission status: Wrong Answer

Solution code:
import math
mod = 10**9+7
N = int(input())
S = input()
cnt = 0
ans = 1
for i in range(N*2):
    if S[i]=='W':
        if cnt%2 == 0:
            ans*=cnt
            cnt-=1
        else:
            cnt+=1
    else:
        if cnt%2 == 0:
            cnt+=1
        else:
            ans*=cnt
            cnt-=1
for i in range(1, N+1):
    ans*=i
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
MOD = 10**9 + 7
threads = 0
M = 1
for s in S:
    if (threads + int(s=='B')) % 2:
        threads += 1
    else:
        M *= threads
        M %= MOD
        threads -= 1
K = 1
for i in range(1,N+1):
    K *= i
    K %= MOD

print((M*K)%MOD)

Submission status: Wrong Answer

Solution code:
m = 10 ** 9 + 7
n = int(input())
s = input()
if s.count('B') != n or s[0] != 'B' or s[-1] != 'B':
    print(0)
else:
    r = 1
    for i in range(2, n + 1):
        r = (r * i) % m
    p = 0
    for i, c in enumerate(s):
        if int(c == 'W') == i % 2:
            p += 1
        else:
            r = (r * p) % m
            p -= 1
    print(r)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
mod=1000000007
n=int(input())
S=input()
Flag=[0]*(2*n)
for i,s in enumerate(S):
    if i%2:
        Flag[i]=int(s=='W')
    else:
        Flag[i]=int(s=='B')
Acc=list(accumulate(Flag))
Acc_rev=[0]*(2*n)
Acc_rev[-1]=Flag[-1]^1
for i in reversed(range(2*n-1)):
    Acc_rev[i]=Acc_rev[i+1]+(Flag[i]^1)
ans=1
cnt=0
for i,f in enumerate(Flag):
    if f:
        if Acc_rev[i]<=0:
            print(0)
            exit()
    else:
        tmp=Acc[i]-cnt
        if tmp<=0:
            print(0)
            exit()
        ans*=tmp
        ans%=mod
        cnt+=1
for i in range(1,n+1):
    ans*=i
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
import math
mod = 10**9+7
N = int(input())
S = input()
cnt = 0
ans = 1
for i in range(N*2):
    if S[i]=='W':
        if cnt%2 == 0:
            ans*=cnt
            cnt-=1
        else:
            cnt+=1
    else:
        if cnt%2 == 0:
            cnt+=1
        else:
            ans*=cnt
            cnt-=1
print(ans*math.factorial(N)%mod)