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

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()

input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=1

n=I()
s=input()
l,r=0,0

for i in range(n*2):
    t=1 if s[i]=='B' else 0
    if (l-r+t)%2==1:
        l+=1
    else:
        ans*=(l-r)
        ans%=mo
        r+=1
    if i>=n:
        ans*=i-n+1
        ans%=mo
if r-l!=0:
    print(0)
else:
    print(ans)

Solution (Python):
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
count=0
for i in range(2*N):
    if data[i]==1:
        count+=1
if count!=N:
    print(0)
    sys.exit()
for i in range(1,N+1):
    ans=ans*i%mod
print(ans)

Solution (Python):
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    
    return divisors
def examA():
    M, D = LI()
    c = D//10
    ans = 0
    for i in range(2,M+1):
        d = make_divisors(i)

        for j in range(len(d)//2):
            cur0 = d[j*2+1]; cur10 = d[j*2]
            if cur10==1:
                continue
            if cur0<=9:
                if cur10<c:
                    ans +=1

                elif cur10==c and cur0+cur10*10<=D:
                    ans +=1

        for j in range(len(d) // 2):
            cur0 = d[j * 2];cur10 = d[j * 2+1]
            if cur0 == 1:
                continue
            if cur10 <= 9:
                if cur10 < c:
                    ans += 1
                elif cur10 == c and cur0 + cur10 * 10 <= D:
                    ans += 1
        if len(d)%2==1:
            cur0 = d[-1]; cur10 = d[-1]
            if cur10==1:
                continue
            if cur0<=9:
                if cur10<c:
                    ans +=1

                elif cur10==c and cur0+cur10*10<=D:
                    ans +=1

    print(ans)
    return

class Bit():
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
        return
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
        return
def examB():
    N, K = LI()
    A = LI()
    bit = Bit(max(A))
    cur = 0
    for i, a in enumerate(A):
        bit.add(a, 1)
        cur += i + 1 - bit.sum(a)
    As = 0
    A.sort()
    now = 0
    for i in range(1,N):
        if A[i]!=A[i-1]:
            now = i
        As += now

    ans = (As*(K**2) - (As-2*cur)*K)//2
    ans %=mod
    print(ans)
    return

def examC():
    N = I()
    S = SI()
    L = [0]; R= []
    flagL = True
    for i in range(1,2*N):
        if S[i]==S[i-1]:
            if flagL:
                R.append(i)
                flagL = False
            else:
                L.append(i)
                flagL = True
        else:
            if flagL:
                L.append(i)
                flagL = True
            else:
                R.append(i)
                flagL = False

    if len(L)!=N or S[0]=="W" or S[-1]=="W":
        print("0")
        return
    cur = 1
    sumR = [0]*(2*N)
    now = 0
    for k,i in enumerate(R):
        for j in range(now,i):
            sumR[j] = k
        now = i

    for i in range(N-1,-1,-1):
        cur *=(N-sumR[L[i]]-(N-i-1))

        cur %=mod

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

def examE():
    N, H, W = LI()
    d = defaultdict(list)
    HL = [[]for _ in range(H+1)]
    WL = [[]for _ in range(W+1)]
    for _ in range(N):
        r,c,a = LI()
        d[(r,c)].append(a)
        HL[r].append(-a)
        WL[c].append(-a)
    print(d)

    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,inf
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Solution (Python):
mod = 10 ** 9 + 7

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

d = 0
ans = 1
for s in S:
    if (s == "B") ^ (d % 2 != 0):
        d += 1
    else:
        ans = (ans * d) % mod
        d -= 1

if d != 0:
    print(0)
    quit()

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

print(ans)

Solution (Python):
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 and s[0]==s[-1]=="B":
    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)

Solution (Python):
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)]

    
    if(sum(A)!=n):
        print(0)
        return

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

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

Solution (Python):
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]

    
    if(A[0]==0):
        print(0)
        return
    if(sum(A)!=n):
        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()

Solution (Python):
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:
        ans=1
        for i in range(2,n+1):
            ans=ans*i%mod
        print(ans)
        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
    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()

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007





n = I()
s = S()

fac = [1] * (n + 1)
inv = [1] * (n + 1)
for j in range(1, n + 1):
    fac[j] = fac[j-1] * j % mod


ans = 1
l = 0
for i in s:
    if (i == 'B' and l % 2 == 0) or (i == 'W' and l % 2):
        l += 1
    else:
        if l == 0:
            print(0)
            exit()
        ans = ans * l % mod
        l -= 1


if l:
    print(0)
else:
    print(ans * fac[n] % mod)

Solution (Python):
def f_c():
    n = int(input())
    s = input()
    q = 10**9 + 7

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

if __name__ == "__main__":
    f_c()

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

mod = 10 ** 9 + 7

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

else :
    ret = 1
    l = 1
    pre = 0
    for i in range(1, 2 * N) :
        if S[i-1] == S[i] :
            if pre == 0 :
                ret *= l
                ret %= mod
                l -= 1
                pre ^= 1
            else :
                l += 1
                pre ^= 1
        else :
            if pre == 0 :
                l += 1
            else :
                ret *= l
                ret %= mod
                l -= 1
                
    if l != 0 :
        print(0)
    else :
        for i in range(1, N + 1) :
            ret *= i
            ret %= mod
            
        print(ret)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
S = input().rstrip()

R = [None]*(2*N)
odd = False
for i in range(2*N):
    if S[i] == 'W':
        if odd:
            R[i] = 1
            odd = False
        else:
            R[i] = 0
            odd = True
    else:
        if odd:
            R[i] = 0
            odd = False
        else:
            R[i] = 1
            odd = True
if sum(R) != N:
    print(0)
else:
    ans = 1
    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)

Solution (Python):
import sys
input = sys.stdin.readline
ModNum = 10**9+7

def main():
    N = int(input())
    S = input()

    direction = [0] * (2*N)

    line = 0
    for i in range(2*N):
        if S[i]=="B" and line%2==0:
            direction[i] = 1
            line += 1
        elif S[i]=="B" and line%2==1:
            direction[i] = -1
            line -= 1
        elif S[i]=="W" and line%2==0:
            direction[i] = -1
            line -= 1
            if line < 0:
                print(0)
                break
        elif S[i]=="W" and line%2==1:
            direction[i] = 1
            line += 1
    else:
        if line>0:
            print(0)
        else:
            ans = 1
            Sum = 0           
            for i in range(2*N):  
                if direction[i]==1:
                    Sum += 1
                else:
                    ans *= Sum
                    ans %= ModNum
                    Sum -= 1
            for i in range(1,N+1):
                ans *= i
                ans %= ModNum
            print(ans)




if __name__ == "__main__":
    main()

Solution (Python):
mod = 10 ** 9 + 7

N = int(input())
s = raw_input()
ans, cnt, tmp = 1, 0, 0
for i in xrange(1, N*2+1):
    if (cnt & 1) ^ (s[i-1] == 'B'):
        cnt += 1
    else:
        ans = (ans * cnt) % mod
        cnt -= 1
        tmp += 1
for i in xrange(1, N+1):
    ans = ans * i % mod

print(ans if tmp == N else 0)

Solution (Python):
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
else:
    if W_cnt:
        print(0)
    else:
        print(ans * factorial(N) % MOD)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
from collections import *
from math import atan2, pi
from bisect import *
mod = 10**9+7

N = int(input())
S = [int(c == 'B') for c in input()]
if S[0] + S[-1] < 2:
    print(0)
    exit()
for i in range(N):
    S[2*i] ^= 1
if sum(S) != N:
    print(0)
    exit()
cnt, ans = 0, 1
for x in S:
    if x == 0:
        cnt += 1
    else:
        if cnt == 0:
            print(0)
            exit()
        ans = ans * cnt % mod
        cnt -= 1
for i in range(2, N+1):
    ans = ans * i % mod
print(ans)

Solution (Python):
N=int(input())
S=input()
mod=10**9+7
d=[0 for i in range(2*N)]


for i in range(1,2*N):
    if S[i-1]==S[i]:
        d[i]=1-d[i-1]
    else:
        d[i]=d[i-1]
if sum(d)!=N or S[0]=="W" or S[-1]=="W":
    print(0)
    exit()
Li=0
Ri=0
ans=1
for i in range(2*N):
    if d[i]==1:
        ans*=Li-Ri
        ans%=mod
    if d[i]==0:
        Li+=1
    else:
        Ri+=1
for i in range(1,N+1):
    ans=ans*i
    ans%=mod
print(ans)

Solution (Python):
import sys
N = int(input())
S = list(input())
MOD = 10**9+7

if S[0] == 'B':
    Left = 1
    ans = 1
else:
    print(0)
    sys.exit()
    
flag = True
for i in range(1,2*N):
    if flag:
        if S[i-1] != S[i]:
            Left += 1
        else:
            flag = False
            ans *= Left
            ans %= MOD
            Left -= 1
    else:
        if S[i-1] != S[i]:
            ans *= Left
            ans %= MOD
            Left -= 1
        else:
            Left += 1
            flag = True
            
if Left != 0:
    print(0)
    sys.exit()
    
for i in range(1,N+1):
    ans *= i
    ans %= MOD

print(ans)

Solution (Python):
N = int(input())
S = list(input())
L = [1]
cumsumL = [1]
res = 1
rvisited = 0
mod = 10 ** 9 + 7
class Factorial:
  def __init__(self, n, mod):
    self.f=[1]
    for i in range(1, n + 1):
      self.f.append(self.f[-1] * i % mod)
    self.i=[pow(self.f[-1], mod - 2, mod)]
    for i in range(1, n + 1)[: : -1]:
      self.i.append(self.i[-1] * i % mod)
    self.i.reverse()
  def factorial(self, i):
    return self.f[i]
  def ifactorial(self, i):
    return self.i[i]
  def combi(self, n, k):
    return self.f[n] * self.i[n - k] % mod * self.i[k] % mod
f = Factorial(N, mod)
if S[0] == "W" or S[-1] == "W":
  print(0)
  exit(0)
for i in range(1, 2 * N):
  if S[i - 1] == S[i]:
    L.append((L[i - 1] + 1 )% 2)
  else:
    L.append(L[i - 1])
if sum(L) != N:
  print(0)
  exit(0)
for i in range(1, 2 * N):
  cumsumL.append(cumsumL[i - 1] + L[i])
for i in range(1, 2 * N):
  if L[i] == 0:
    res *= cumsumL[i] - rvisited % mod
    res %= mod
    rvisited += 1
print(f.factorial(N) * res % mod)

Solution (Python):
mod = 10 ** 9 + 7

n = int(input())
s = input()
t = [(ind % 2) ^ (ss == 'W') for ind, ss in enumerate(s)]


ans = 1
zeros = 0
for tt in t:
    if tt == 0:
        zeros += 1
    else:
        if zeros == 0:
            print(0)
            exit()
        ans = ans * zeros % mod
        zeros -= 1

if zeros > 0:
    print(0)
    exit()

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

print(ans)

Solution (Python):
mod = 10 ** 9 + 7

n = int(input())
s = input()
t = list(map(lambda x: 1 if x == 'B' else 0, s))
t = tuple((tt - df) % 2 for tt, df in zip(t, reversed(range(n * 2))))


ans = 1
zeros = 0
ones = 0
for tt in t:
    if tt == 0:
        zeros += 1
    else:
        if zeros == 0:
            print(0)
            exit()
        ans *= zeros
        ans %= mod
        zeros -= 1
        ones += 1

if zeros > 0:
    print(0)
    exit()

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

print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)


def fact(x):
    if x == 0:
        return 1
    else:
        return x * fact(x - 1) % mod


mod = 10 ** 9 + 7

n = int(input())
s = input()
t = list(map(lambda x: 1 if x == 'B' else 0, s))
t = tuple((tt - df) % 2 for tt, df in zip(t, reversed(range(n * 2))))


zeros = 0
ans = 1
ones = 0
for tt in t:
    if tt == 0:
        zeros += 1
    else:
        if zeros == 0:
            print(0)
            exit()
        ans *= zeros
        ans %= mod
        zeros -= 1
        ones += 1

if zeros > 0:
    ans = 0

ans *= fact(ones)
ans %= mod
print(ans)

Solution (Python):
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
    flag = True
    cnt = 1
    ret = 1
    for i in range(1, 2 * n):
        if s[i - 1] == s[i]:
            flag = not flag
        if flag:
            cnt += 1
        else:
            ret *= cnt
            ret %= divider
            cnt -= 1
    if not cnt == 0:
        print(0)
        return
    for i in range(n):
        ret *= i + 1
        ret %= divider
    print(ret)
    return


if __name__ == "__main__":
    main()

Solution (Python):
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
    lr = [True]
    for i in range(1, 2 * n - 1):
        if s[i - 1] == s[i]:
            lr.append(not lr[i - 1])
        else:
            lr.append(lr[i - 1])
    lr.append(False)
    ret = 1
    cnt = 0
    for i in range(2 * n):
        if lr[i]:
            cnt += 1
        else:
            ret *= cnt
            ret %= divider
            cnt -= 1
    if not cnt == 0:
        print(0)
        return
    for i in range(n):
        ret *= i + 1
        ret %= divider
    print(ret)
    return


if __name__ == "__main__":
    main()

Solution (Python):
import math

n = int(input())
cell = map(lambda x: {"B": True, "W": False}[x], input())
section = 0
ans = 1
for val in cell:
    if section % 2 == 0:
        buf = val
    else:
        buf = not val
    if buf:
        section += 1
    else:
        ans *= section
        ans %= 10 ** 9 + 7
        section -= 1
if section == 0:
    print(((math.factorial(n) % (10 ** 9 + 7)) * ans) % (10 ** 9 + 7))
else:
    print(0)

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

mod = 10 ** 9 + 7

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

ans = 1
prev_s = S[0]
prev_t = True
t_sum = 1
f_sum = 0
for i in range(1, 2*N-1):
    if S[i] == prev_s:
        t = not prev_t
    else:
        t = prev_t

    if t:
        t_sum += 1
    else:
        ans *= (t_sum - f_sum)
        ans %= mod
        f_sum += 1
    prev_s = S[i]
    prev_t = t

if t_sum != N:
    ans = 0

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

print(ans)

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

mod = 10 ** 9 + 7

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

ans = 1
prev_s = S[0]
prev_t = True
t_sum = 1
f_sum = 0
for i in range(1, 2*N-1):
    if S[i] == prev_s:
        t = not prev_t
    else:
        t = prev_t

    if t:
        t_sum += 1
    else:
        ans *= (t_sum - f_sum)
        ans %= mod
        f_sum += 1
    prev_s = S[i]
    prev_t = t

if t_sum != N:
    ans = 0

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

print(ans)

Solution (Python):
import sys
mod=1000000007
n=int(input())
s=str(input())
ans=1
for i in range(1,n+1):
  i=i%mod
  ans=ans*i
  ans=ans%mod
d={"B":1,"W":0}
sg=[]
now=1
sss=0
for i in range(2*n):
  if d[s[i]]==now%2:
    sss+=1
    now+=1
    a="S"
  else:
    a="G"
    now-=1
    if sss>=0:
      ans*=sss
      ans=ans%mod
      sss-=1
    else:
      ans=0
  
if sss==0:
  print(ans)
else:
  print(0)

Solution (Python):
MOD = 10**9 + 7

N = int(input())
S = input()
ok = (S[0] == S[-1] == 'B')
if ok:
    ans = 1
    cur = 0
    prv = 'W'
    p = 1
    for c in S:
        if prv == c:
            if p & 1:
                ans = ans * cur % MOD
                cur -= 1
            else:
                cur += 1
            p ^= 1
        else:
            if p & 1:
                cur += 1
            else:
                ans = ans * cur % MOD
                cur -= 1
            prv = c
    if cur != 0:
        ok = 0
    for i in range(1, N+1):
        ans = ans * i % MOD
if ok:
    print(ans)
else:
    print(0)

Solution (Python):


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

L,R = [0], []
total = [0 for i in range(2*N)]
total[0] = 1
l,r = 1,0
prev = "L"
for i in range(1, 2*N):
    if S[i] == S[i-1]:
        if prev == "L":
            R.append(i)
            total[i] = total[i-1] - 1
            r += 1
            prev = "R"
        else:
            L.append(i)
            total[i] = total[i-1] + 1
            l += 1
            prev = "L"
    else:
        if prev == "L":
            L.append(i)
            total[i] = total[i-1] + 1
            l += 1
        else:
            R.append(i)
            total[i] = total[i-1] - 1
            r += 1
    if l < r:
        print(0)
        exit()

if not (len(L) == N and len(R) == N):
    print(0)
    exit()

def fact(n):
    ret = 1
    while n > 1:
        ret = (ret * n) % MOD
        n -= 1
    return ret
    
cnt = 1
for r in R:
    cnt = (cnt * (total[r] + 1)) % MOD
print(cnt * fact(N) % MOD)

Solution (Python):
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

if W_cnt > 0:
    print(0)
else:
    print(ans * factorial(N) % MOD)

Solution (Python):
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) % mod)

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

if not store == 0:
    print(0)
    sys.exit()

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

Solution (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

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


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

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

    else:
        return True


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

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


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

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


if judge(bw, lr):
    cnt = 0
    ans = 1
    for lri in lr:
        if lri == "L":
            cnt += 1
        else:
            ans *= cnt
            ans %= MOD
            cnt -= 1

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

Solution (Python):
import math
import os
mod = 10**9 + 7
N = int(input())
S = input()

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

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

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

Solution (Python):
n = int(input())
s = input()
d = [0 for _ in range(2*n)]
ans = 1
cnt_l = 1
cnt_r = 0
d[0] = "L"
for i in range(1, 2*n):
    if s[i] != s[i-1]:
        d[i] = d[i-1]
    elif d[i-1] == "R":
        d[i] = "L"
    else:
        d[i] = "R"
    
    if d[i] == "L":
        cnt_l += 1
    elif d[i] == "R":
        ans = ans * (cnt_l - cnt_r) % (10**9 + 7)
        cnt_r += 1

import math
ans = math.factorial(n) % (10**9 + 7) * ans

if cnt_r == cnt_l and s[0] != "W" and s[-1] != "W":
    print(ans %(10**9 + 7))
else:
    print(0)

Solution (Python):
def f_c():
    n = int(input())
    s = input()
    q = 10**9+7

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

if __name__ == "__main__":
    f_c()

Solution (Python):


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