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):
mod = 10**9+7

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

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

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

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

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

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

print(ans)

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

lr = []
rc = 0
for i,c in enumerate(S):
    isr = (i%2) ^ int(c=='W')
    lr.append(isr)
    rc += isr

if rc != N:
    print(0)
    exit()

l = 0
ans = 1
for isr in lr:
    if isr:
        if l:
            ans *= l
            ans %= MOD
            l -= 1
        else:
            print(0)
            exit()
    else:
        l += 1

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

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

N = len(S)//2
MOD = 10**9+7

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

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

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

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 1; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]

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

lr = ""
for i in range(2 * n):
    if new_s[i] == "B":
        lr += "R"
    else:
        lr += "L"

if lr.count("R") != n:
    print(0)
    exit()

lcount = 0
for i in range(2 * n):
    if lr[i] == "L":
        lcount += 1
    else:
        ans *= lcount
        ans %= mod
        lcount -= 1

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

print(ans)

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

def factorial(n):
    res = 1
    for p in range(2,n+1):
        res *= p
        res %= mod
    return res

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

T = "L"

L = 0
R = 0

for i in range(1,2*N):
    if S[i] == "W":
        if (L - R + 1) % 2 == 0:
            R += 1
            T += "R"
        else:
            L += 1
            T += "L"
    else:
        if (L - R + 1) % 2 == 0:
            L += 1
            T += "L"
        else:
            R += 1
            T += "R"

if T[-1] == "L" or T.count("L") > N:
    print(0)
    exit()

L = 0
ans = 1

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

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

Solution (Python):
import math

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

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

if left > 0:
    print(0)
    exit()
            
for i in range(1, N+1):
    ret *= i
    ret %= mod
print(ret)

Solution (Python):
import sys
n = int(input())
s = input()
if s[0] == "W":
  print(0)
  sys.exit()
MOD = 10**9+7
ans = 1
cnt = 1
right = 1
for i in range(2*n-1):
  if s[i+1] == s[i]:
    right ^= 1
  if right:
    cnt += 1
  else:
    ans *= cnt
    ans %= MOD
    cnt -= 1
if cnt != 0:
  print(0)
else:
  for i in range(1, n+1):
    ans *= i
    ans %= MOD
  print(ans)

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

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

Solution (Python):

MOD = 10**9 + 7
n = int(input())
s = input()
d = 0
ans = 1
for i in range(2 * n):
    if (s[i] == 'B') ^ (d % 2 != 0):
        d += 1
    else:
        ans *= d
        ans %= MOD
        d -= 1
if d != 0:
    print(0)
    exit()
for i in range(1, n + 1):
    ans *= i
    ans %= MOD
print(ans)

Solution (Python):
from math import factorial

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

MOD = 10 ** 9 + 7

def swap(char):
    if char == 'W':
        return 'B'
    else:
        return 'W'

cnt = 0
for i in range(2 * N):
    if i % 2 == 0:
        S[i] = swap(S[i])
    if S[i] == 'B':
        cnt += 1

if cnt != N or S[0] == 'B' or S[-1] == 'W':
    print(0)
else:
    rest_W = 0
    ans = 1
    for i in range(2 * N):
        if S[i] == 'W':
            rest_W += 1
        if S[i] == 'B':
            ans *= rest_W
            ans %= MOD
            rest_W -= 1
    ans = (ans * factorial(N) % MOD)
    print(ans)

Solution (Python):
N = int(input())
S = input()
MOD = 10**9+7
A = [0 if S[i]=='W' else 1 for i in range(2*N)]
A = [(A[i]+i+1)%2 for i in range(2*N)]


ans = 1
l = 0
r = N
flag = True
for i in range(2*N):
    if A[i] == 0:
        l += 1
    
    else:
        ans = (ans*l)%MOD
        r -= 1
        l -= 1
        if l < 0:
            flag = False
            break
if r != 0:
    flag = False
if not flag:
    ans = 0
for i in range(1,N+1):
    ans = (ans*i)%MOD

print(ans)

Solution (Python):
N = int(input())
S = input()
MOD = 10**9+7
A = [0 if S[i]=='W' else 1 for i in range(2*N)]
A = [(A[i]+i+1)%2 for i in range(2*N)]


ans = 1
l = 0
r = N
flag = True
for i in range(2*N):
    if A[i] == 0:
        l += 1
    
    else:
        ans = (ans*l)%MOD
        r -= 1
        l -= 1
        if l < 0:
            flag = False
            break
if r != 0:
    flag = False

for i in range(1,N+1):
    ans = (ans*i)%MOD
if not flag:
    ans = 0
print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    m,d = LI()
    ans = 0
    for i in range(1,m+1):
        for j in range(1,d+1):
            d1 = j%10
            d2 = j//10
            if d1 >= 2 and d2 >= 2 and d1*d2 == i:
                ans += 1
    print(ans)
    return


def B():
    def add(i):
        while i <= m:
            bit[i] += 1
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    n,k = LI()
    m = 2000
    a = LI()
    bit = [0]*(m+1)
    ans = 0
    for i in range(n):
        ans += (i-sum(a[i]))*k
        add(a[i])
    inv = pow(2,mod-2,mod)
    for i in range(n):
        ans += sum(a[i]-1)*k*(k-1)*inv%mod
    print(ans%mod)
    return


def C():
    n = I()
    s = S()
    if "W" in (s[0],s[-1]):
        print(0)
        return
    d = [0]
    for i in range(1,2*n):
        if s[i] == s[i-1]:
            d.append(1-d[-1])
        else:
            d.append(d[-1])
    if sum(d) != n:
        print(0)
        return
    ans = 1
    s = 0
    for i in d:
        if not i:
            s += 1
        else:
            ans *= s
            s -= 1
            ans %= mod
    for i in range(1,n+1):
        ans *= i
        ans %= mod
    print(ans)
    return


def D():
    n = I()
    m = n.bit_length()
    v = [[0]*n for i in range(n)]
    for i in range(n):
        for j in range(n):
            k = i^j
            for l in range(m):
                if k&(1<<l):
                    v[i][j] = l+1
                    break
    for i in range(n-1):
        print(*v[i][i+1:])
    return


def E():

    return


def F():

    return


if __name__ == "__main__":
    C()

Solution (Python):
n = int(input())
s = input()
MOD = 10**9 + 7
li = []
for i in range(2*n):
    if i % 2 == 0 and s[i] == "B":
        li.append("W")
    elif i % 2 == 0 and s[i] == "W":
        li.append("B")
    elif i % 2 == 1 and s[i] == "B":
        li.append("B")
    elif i % 2 == 1 and s[i] == "W":
        li.append("W")
        
cnt = 0
ans = 1
for string in li:
    if string == "W":
        cnt += 1
    else:
        if cnt <= 0:
            print(0)
            exit()
        ans *= cnt
        cnt -= 1
    ans %= MOD

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

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

print(ans)

Solution (Python):
def main():
  import math
  import sys
  imput=sys.stdin.readline
  a=int(input())
  b=input()
  R=0
  L=0
  ans=1
  mod=10**9+7
  if b[0]=="W" or b[-1]=="W":
    print(0)
    exit()
  for i in range(2*a):
    if i==0:
      L+=1
      fr="L"
    else:
      if b[i]==b[i-1]:
        if fr=="L":
          ans=ans*(L-R)%mod
          R+=1
          fr="R"
        else:
          L+=1
          fr="L"
      else:
        if fr=="L":
          L+=1
        else:
          ans=ans*(L-R)%mod
          R+=1
    
  if L!=R:
    print(0)
    exit()


  
  print(ans*math.factorial(a)%mod)
  

if __name__ == '__main__':  
  main()

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

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

print(ans)

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

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

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

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

if __name__ == '__main__':

    print(main())

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

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

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

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

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

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

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


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

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

ans *= kai[n]
ans %= MOD

print(ans)

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

def solve():
  N = int(input())
  S = [s == 'W' for s in input()] 
  if S[0] or S[2 * N - 1]: return 0

  ans = 1
  cnt = 1
  d = True 
  for i in range(1, N * 2):
    if S[i] == S[i - 1]: d = not d
    if d: cnt += 1
    else:
      ans = (ans * cnt) % MOD
      cnt -= 1

  if cnt != 0: return 0

  for i in range(2, N + 1): ans = (ans * i) % MOD
  return ans

print(solve())

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

def solve():
  N = int(input())
  S = [s == 'W' for s in input()] 
  if S[0] or S[2 * N - 1]: return 0

  ans = 1
  cnt = 1
  d = True 
  for i in range(1, N * 2):
    if S[i] == S[i - 1]: d = not d
    if d: cnt += 1
    else:
      ans = (ans * cnt) % MOD
      cnt -= 1

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

  return ans if cnt == 0 else 0

print(solve())

Solution (Python):
import math

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

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

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

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

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

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

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

if num > 0:
    ans = 0

ans *= kai[n]
ans %= MOD

print(ans)

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


    if numL != 0:
        return 0

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

    return ans*fact%MOD


ans = solve()
print(ans)

Solution (Python):
MOD = int(1e9+7)
N = int(input())
S = input()
LR = [0]*(2*N)
LR[0] = 0
l, r = 1, 0
if S[0] == "W" or S[-1] == "W":
    print(0)
    exit()
for i in range(1, 2*N):
    if S[i-1] == S[i]:
        LR[i] = 0 if LR[i-1] == 1 else 1
    else:
        LR[i] = LR[i-1]
    if LR[i] == 0:
        l += 1
    else:
        r += 1
if l != r:
    print(0)
    exit()
L = 0
R = 0
count = 1
for w in LR:
    if w == 0:
        L += 1
    else:
        count *= L - R
        count %= MOD
        R += 1
for i in range(1, N+1):
    count = count * i % MOD
print(count)

Solution (Python):
N = int(input())
S = input()
ss = [1 if s == 'W' else 0 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

if num > 0:
    ans = 0

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

print(ans)

Solution (Python):
MOD = 10**9 + 7
n = int(input())
s = input()
ans = 1
left = 0
for i in s:
    if i == "B":
        if left%2 == 1:
            ans *= left
            left -= 1
        else:
            left += 1
    else:
        if left%2 == 0:
            ans *= left
            left -= 1
        else:
            left += 1
    if left < 0:
        print(0)
        exit()
    ans %= MOD

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

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

print(ans)

Solution (Python):
import math
N = int(input())
S = input()
ans = 1
ans_base = 1
numofS = 0  
line = 0  
for i in range(2*N):
    if S[i] == "B":
        if line % 2 == 0:
            numofS += 1
            line += 1
        else:
            ans_base *= line
            line -= 1
    else:
        if line % 2 == 0:
            ans_base *= line
            line -= 1
        else:
            line += 1
            numofS += 1

    if line < 0:
        ans = 0
        break
    ans_base %= 10**9+7
if numofS != N:
    ans = 0
    

if ans != 0:
    fact = math.factorial(N)
    fact %= 10**9+7
    ans = ans_base * fact
    ans %= 10**9+7
print(ans)

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

LR = [None] * (N * 2)
left_left = 0
ans = 1
for i in range(2 * N):
    if i:
        LR[i] = LR[i - 1] ^ (S[i - 1] == S[i])
    else:
        LR[i] = S[i] == "B"
    if LR[i]:
        left_left += 1
    else:
        ans *= left_left
        ans %= MOD
        left_left -= 1
if left_left != 0:
    ans = 0
for i in range(1, N + 1):
    ans *= i
    ans %= MOD

print(ans)

Solution (Python):

import sys
import math
from collections import defaultdict


sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]


mod = 10**9 + 7


def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))



def fact(a,M=mod):
    ans = 1
    for i in range(2,a+1):
        ans = ans*i
        ans = ans%M
    return ans

N = I()
S = str(input())

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

if S[2*N-1]=='W':
    print(0)
    exit()

d = ['L']*(2*N)

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

nr = 0
nl = 0
for i in range(2*N):
    if d[i]=='L':
        nl += 1
    else:
        nr += 1
if nl != nr:
    print(0)
    exit()



temp = 0
x = [0]*(2*N)
for i in range(2*N):
    if d[i]=='L':
        temp += 1
    else:
        x[i] = temp

ans = 1
rnum = 0
for i in range(2*N):
    if d[i]=='R':
        ans *= x[i]-rnum
        ans %= mod
        rnum += 1

ans *= fact(N)
ans %= mod

print(ans)

Solution (Python):
from collections import Counter
mod = 1000000007


def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def main():
    n = int(input())
    s =input()
    frac = [1] * (n+1)
    for i in range(n):
        frac[i+1] = frac[i] * (i+1) % mod
    cnt = [0, 1]
    f = 0
    a = [0] * (2* n)
    a[-1] = 1
    for i in range(2*n-1):
        a[i] = f
        if s[i] == s[i+1]:
            f ^= 1

    ans = 1
    tmp = 0
    for i in range(2*n):
        if a[i] == 0:
            tmp += 1
        else:
            ans *= tmp
            ans %= mod
            tmp -= 1

    if len([i for i in a if i == 0]) == n and s[0] == s[-1] == 'B':
        print(ans * frac[n] % mod)
    else:
        print(0)

main()

Solution (Python):

M = 10**9+7
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]
if (sum(a) != sum(b)) | (b[-1] == 0):
    print(0); exit()
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; c %= M
for i in range(1, n+1):
    c = c*i%M
print(c)

Solution (Python):
n=int(input())
s=input()
a=[]
p=10**9+7
for c in s:
    if(c=="B"):
        a.append(-1)
    else:
        a.append(1)

kako=[]

mi=0
kanou=True

for i in range(2*n):
    temp=1
    if(mi%2==1):
        if(a[i]==-1):
            temp=-1
    if(mi%2==0):
        if(a[i]==1):
            temp=-1
    mi+=temp
    if(mi<0):
        kanou=False
    kako.append(temp)

if(mi!=0):
    kanou=False
ans=1
rui=0
if(kanou):
    for i in range(2*n):
        if(kako[i]==1):
            rui+=1
        else:
            ans*=rui
            rui-=1
            ans=ans%p
    for i in range(1,n+1):
        ans*=i
        ans=ans%p
    print(ans)
else:
    print(0)

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

A = []
cnt = 1
l = 0
pc = "W"
d = 1
for c in S:
    if c==pc:
        cnt += 1
        A.append(l)
    else:
        if cnt%2==1:
            pass
        else:
            d = -d
        l += d
        cnt = 1
        A.append(l)
    pc = c

if not (A[0]==A[-1]==1 and 0 not in A):
    print(0)
    exit()
mod = 10**9+7
ans = 1
pa = 1
d = 1
n = 1
for a in A[1:]:
    if pa!=a:
        pass
    else:
        d *= -1
    if d==-1:
        ans *= n
        ans %= mod
        n -= 1
    else:
        n += 1
    pa = a
for i in range(2, N+1):
    ans = ans * i % mod
print(ans)

Solution (Python):
m = 10 ** 9 + 7
n = int(input())
s = input()
r, p = 1, 0
for i in range(2, n + 1):
    r = (r * i) % m
for i, c in enumerate(s):
    if int(c == 'W') == i % 2:
        p += 1
    else:
        r = (r * p) % m
        p -= 1
print(r if p == 0 else 0)

Solution (Python):
N = int(input())
S = list(input())
MOD = 10**9+7
BW = "BW"
ans = 1
hoge = 0
for i in range(2*N):
    if S[i] == BW[i%2]:
        hoge += 1
    else:
        ans *= hoge
        ans %= MOD
        hoge -= 1
if hoge != 0:
    ans = 0
for i in range(1,N+1):
    ans *= i
    ans %= MOD
print(ans)

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


from collections import deque
a = deque()
a.append((0,1))
ans = 0
for i,c in enumerate(list(s)):
    if i == 2*n-1:
        for q,w in a:
            ans += w
            ans %= mod
        break
    b = deque()
    while a:
        q,w = a.pop()
        
        
        if c=="W":
            
            if q%2==0:
                if q == 0:continue
                w *= q
                w %= mod
                q -= 1
                b.append((q,w))
            else:
                if 2*n-i <= q:continue
                q += 1
                b.append((q,w))
        else:
            if q%2==0:
                if 2*n-i <= q:continue
                q += 1
                b.append((q,w))
            else:
                if q==0:continue
                w *= q
                w %= mod
                q -= 1
                b.append((q,w))
    a = b
a = 1
for i in range(1, n+1):
    a *= i
    a %= mod
ans = ans*a % mod
print(ans)

Solution (Python):
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))
if Acc[-1]!=n or Flag[-1]==1:
    print(0)
    exit()
ans=1
cnt=0
for i,f in enumerate(Flag):
    if f==0:
        tmp=Acc[i]-cnt
        ans*=tmp
        ans%=mod
        cnt+=1
for i in range(1,n+1):
    ans*=i
    ans%=mod
print(ans)

Solution (Python):


MOD = int(10**9 + 7)

def f(S):
    st = 0
    cnt = 0
    x = 1
    for i, c in enumerate(S):
        if i%2 == 0 and c == 'B' or i%2 == 1 and c == 'W':
            st += 1
            cnt += 1
        else:
            if st == 0:
                return 0
            x = x*st%MOD;
            st -= 1

    if st > 0:
        return 0

    for i in range(1, cnt+1):
        x = x*i%MOD

    return x



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

    print(f(S))


if __name__ == '__main__':
    main()

Solution (Python):
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))
if Acc[-1]!=n:
    print(0)
    exit()




ans=1
cnt=0



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)

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

r = 1
i = 0
nw = 0
nb = 0
while i < 2*N:
  j = S.find("B", i)
  if j < 0:
    j = 2*N
  k = S.find("W", j)
  if k < 0:
    k = 2*N
  
  nw += j-i
  nb += k-j
  for i in range(nb):
    r = r*nw%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)

Solution (Python):
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
if l!=0:
    a=0
for i in range(1,N+1):
    a=a*i%(10**9+7)
print(a)

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

if num > 0:
    ans = 0

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

print(ans)

Solution (Python):
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
thread=1 if s[0]=="B" else 0
for i in range(1, 2*n):
    cnt=i+1 if i<n else 2*n-i
    if s[i]=="B":
        if thread%2==0:
            thread+=1
        else:
            ans*=thread
            thread-=1
    elif s[i]=="W":
        if thread%2==0:
            ans*=thread
            thread-=1
        else:
            thread+=1
    ans%=mod

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

Solution (Python):
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
from itertools import permutations,groupby
import sys
import bisect
import string
alp=string.ascii_lowercase
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
mo=10**9+7



n=I()
s=list(input())
ans=0
t=[]
l,r=0,0
for i in range(n*2):
    if i==0:
        t+=['L']
        l+=1
        continue
    if ((l-r)%2==1 and s[i]=='W') or ((l-r)%2==0 and s[i]=='B'):
        t+=['L']
        l+=1
    else:
        t+=['R']
        r+=1
if t.count('L')!=n:
    print(0)
    exit()
    

L,R=0,0
ans=1
for i in t:
    if i=='L':
        L+=1
    if i=='R':
        ans*=L-R
        ans%=mo
        R+=1
f=1
for i in range(2,n+1):
    f*=i
    f%=mo
print((ans*f)%mo)