Problem:


Score : 500 points

Problem Statement

There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.

The color of the i-th square from the left is black if the i-th character of S is B, and white if that character is W.

You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.  

Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.

Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.

Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.

Constraints

1 \leq N \leq 10^5

|S| = 2N

Each character of S is B or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.

Sample Input 1

2
BWWB

Sample Output 1

4

There are four ways to make all the squares white, as follows:

Choose Squares 1, 3 in the first operation, and choose Squares 2, 4 in the second operation.

Choose Squares 2, 4 in the first operation, and choose Squares 1, 3 in the second operation.

Choose Squares 1, 4 in the first operation, and choose Squares 2, 3 in the second operation.

Choose Squares 2, 3 in the first operation, and choose Squares 1, 4 in the second operation.

Sample Input 2

4
BWBBWWWB

Sample Output 2

288

Sample Input 3

5
WWWWWWWWWW

Sample Output 3

0



Sample input:
2
BWWB


Sample output:
4

Submission status: Wrong Answer

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

a = [0]*(2*n)
cnt = 0
for i in range(2*n):
    if s[i] == 'B':
        if cnt % 2 == 0:
            a[i] = 1
            cnt += 1
        else:
            a[i] = -1
            cnt -= 1
    else:
        if cnt % 2 == 0:
            a[i] = -1
            cnt -= 1
        else:
            a[i] = 1
            cnt += 1
pl = 0
judge = False
mod = 10**9+7
#print(a)

res = 1
for e in a:
    if e > 0:
        pl += 1
    else:
        if pl <= 0:
            judge = True
            break
        res = res*pl%mod
        pl -= 1

if judge:
    print(0)
    exit()

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

Submission status: Wrong Answer

Solution code:
import math


N = int(input())

S = input()

now = 0

ans = 1

for i in range(2*N):

    #print (S[i],ans,now)

    if i == 0 and S[i] == "W":
        ans = 0
        break

    if i == 0 and S[i] == "B":
        now += 1
        continue

    if S[i] == "W":

        if now % 2 != 0:

            now += 1

        else:

            ans *= now
            now -= 1

    if S[i] == "B":

        if now % 2 != 0:

            ans *= now

            now -= 1

        else:

            now += 1


    if now < 0:
        ans = 0
        break

    #print (S[i],ans,now)

if now != 0:
    ans = 0

print (math.factorial(N) * ans)

Submission status: Wrong Answer

Solution code:
import re
n = int(input())
s = input()
print(4 * len(re.findall(r"BW{2,}B", s)) % (10**9 + 7))

Submission status: Wrong Answer

Solution code:
N = int(input())

S = input()

mod = 10**9+7

count = 0
ans = 1
for i in range(2*N):
        if count==0:
                if S[i]=="W":
                        ans=0
                        break
                else:
                        count+=1
        else:
                if S[i]=="W":
                        if count%2==1:
                                count+=1
                        else:
                                ans*=count
                                ans%=mod
                                count-=1
                else:
                        if count%2==1:
                                ans*=count
                                ans%=mod
                                count-=1
                        else:
                                count+=1


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


print(ans)

Submission status: Wrong Answer

Solution code:
import re
n = int(input())
s = input()
print(4 * len(re.findall(r"BW{2,}B", s)))

Submission status: Wrong Answer

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

m = 1

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

if s[0]=='B' and s[-1]=='B':
    if s.count('B')%2==0:
        print(n*(n-1)*m%(10**9+7))
        exit()
print(0)

Submission status: Wrong Answer

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

t = 0
for a,b in zip(S,S[1:]):
    if a!=b:
        t += 1

ans = t*(t-1)
ans %= MOD

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意
 
n = int(input())
#n,k = [int(i) for i in readline().split()]
#a = [int(i) for i in readline().split()]
s = input()

#1: 大きいのとつながる 

res = [(i&1)^(1 if c == "B" else 0) for i,c in enumerate(s)]
#print(res)

ans = 1
num = 0
MOD = 10**9+7
for i in res:
    if i: num += 1
    else:
        ans = ans*num%MOD
        num -= 1

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

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(N: int, S: str):
    tmp = 0
    p = 1
    for c in S:
        if c == 'B':
            if tmp % 2 == 0:
                tmp += 1
            elif tmp > 0:
                p *= tmp
                p %= MOD
                tmp -= 1
            else:
                print(0)
                return
        if c == 'W':
            if tmp % 2 == 1:
                tmp += 1
            elif tmp > 0:
                p *= tmp
                p %= MOD
                tmp -= 1
            else:
                print(0)
                return
    ret = 1
    for i in range(1, N + 1):
        ret *= i
        ret %= MOD
    ret *= p
    ret %= MOD
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
fn=1
for i in range(1,n+1):
  fn*=i
  fn%=1000000007
s=input()
ans=0
for i in s:
  if i=='b':
    ans+=1
print(ans*(ans-1)//2*fn%1000000007)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9 + 7

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

is_left = [(i%2 == 0) ^ (x == 'W') for i,x in enumerate(S)]

answer = 1
left = 0
for bl in is_left:
    if bl:
        left += 1
    else:
        answer *= left
        left -= 1
        answer %= MOD

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

print(answer)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
S = list(input())

dp = [0]*(2*N)

cnt = 0
ans = 1
for i in range(2*N):
    s = S[i]
    if s == 'B':
        if cnt%2 == 0:
            cnt += 1
            ans *= cnt
        else:
            cnt -= 1
    elif s == 'W':
        if cnt%2 == 0:
            cnt -= 1
        else:
            cnt += 1
            ans *= cnt
    ans %= mod


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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
S = input()
defi = 10**9 + 7

ans = 0
if S[0] != "W" or S[-1] != "W":

  LRlist = [""]*len(S)
  LRlist[0] = "L"
  for i in range(1,len(S)):
    if S[i-1] == S[i]:
      if LRlist[i-1] == "L":
        LRlist[i] = "R"
      else:
        LRlist[i] = "L"
    else:
      LRlist[i] = LRlist[i-1]
      
  if LRlist.count("L") == LRlist.count("R"):

    countL = 0
    selectedL = 0
    numOfComb = 1
    for j in range(len(LRlist)):
      if LRlist[j] == "R":
        numOfComb *= (countL - selectedL)
        selectedL += 1
      else:
        countL += 1
    
    ans = (numOfComb * math.factorial(N)) % defi
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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
    rvisited += 1
print(f.factorial(N) * res % mod)

Submission status: Time Limit Exceeded

Solution code:
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
        section -= 1
if section == 0:
    print(((math.factorial(n) % (10 ** 9 + 7)) * (ans % (10 ** 9 + 7))) % (10 ** 9 + 7))
else:
    print(0)

Submission status: Time Limit Exceeded

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

mod = 10 ** 9 + 7

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

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

    if T[i]:
        t_sum += 1
    else:
        ans *= (t_sum - f_sum)
        f_sum += 1

if t_sum != N:
    ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
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
ss=0
gg=0
for i in range(n):
  if d[s[i]]==now%2:
    sg.append("S")
    now+=1
    ss+=1
  else:
    sg.append("G")
    now-=1
    gg+=1
now=1
sg1=[]
for i in range(1,n+1):
  #print("now",now,s[-1*i])
  if d[s[-1*i]]==now%2:
    sg1.insert(0,"G")
    now+=1
    gg+=1
  else:
    sg1.insert(0,"S")
    now-=1
    ss+=1
sg2=sg+sg1
#print(sg2)
sss=0
for i in sg2:
  if i=="S":
    sss+=1
  else:
    if sss>=0:
      ans*=sss
      ans=ans%mod
      sss-=1
    else:
      print(0)
      sys.exit()
  #print(i,ans,sss)      
if sss==0:
  print(ans)
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
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 *= n
        n -= 1
    return ret
    
cnt = 1
for r in R:
    cnt = (cnt * (total[r] + 1)) % MOD
print(cnt * fact(N) % MOD)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
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], []
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)
            r += 1
            prev = "R"
        else:
            L.append(i)
            l += 1
            prev = "L"
    else:
        if prev == "L":
            L.append(i)
            l += 1
        else:
            R.append(i)
            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 *= n
        n -= 1
    return ret
    
R.reverse()
R_border = 0
cnt = 1
l = N-1
while l >= 0:
    while R_border < N-1 and L[l] < R[R_border+1]:
        R_border += 1
    cnt = cnt * (R_border + 1 - (N-1 - l)) % MOD
    l -= 1
print(cnt * fact(N) % MOD)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
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], []
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)
            r += 1
            prev = "R"
        else:
            L.append(i)
            l += 1
            prev = "L"
    else:
        if prev == "L":
            L.append(i)
            l += 1
        else:
            R.append(i)
            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 *= n
        n -= 1
    return ret
    
R.reverse()
R_border = 0
cnt = 1
l = N-1
while l >= 0:
    while R_border < N-1 and L[l] < R[R_border+1]:
        R_border += 1
    cnt = cnt * (R_border + 1 - (N-1 - l)) % MOD
    l -= 1
print(cnt * fact(N) % MOD)

Submission status: Time Limit Exceeded

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

rl=["L"]
for i in range(1,2*N):
  if S[i]==S[i-1]:
    if rl[i-1]=="R":
      rl.append("L")
    elif rl[i-1]=="L":
      rl.append("R")
  elif S[i]!=S[i-1]:
    if rl[i-1]=="R":
      rl.append("R")
    elif rl[i-1]=="L":
      rl.append("L")

if rl.count("L")!=N or S[0]=="W" or S[2*N-1]=="W":
  print(0)
  exit()

ans=1
for i in range(2*N):
  if rl[i]=="R":
    if rl[:i].count("L")-rl[:i].count("R")<0:
      print(0)
      exit()
    else:
      ans=ans%mod*(rl[:i].count("L")%mod-rl[:i].count("R")%mod)%mod

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

Submission status: Time Limit Exceeded

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

rl=["L"]
for i in range(1,2*N):
  if S[i]==S[i-1]:
    if rl[i-1]=="R":
      rl.append("L")
    elif rl[i-1]=="L":
      rl.append("R")
  elif S[i]!=S[i-1]:
    if rl[i-1]=="R":
      rl.append("R")
    elif rl[i-1]=="L":
      rl.append("L")

if rl.count("L")!=N or S[0]=="W" or S[2*N-1]=="W":
  print(0)
  exit()

ans=1
for i in range(2*N):
  if rl[i]=="R":
    if rl[:i].count("L")-rl[:i].count("R")<0:
      print(0)
      exit()
    else:
      ans*=(rl[:i].count("L")%mod-rl[:i].count("R")%mod)%mod

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

Submission status: Time Limit Exceeded

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

rl=["L"]
for i in range(1,2*N):
  if S[i]==S[i-1]:
    if rl[i-1]=="R":
      rl.append("L")
    elif rl[i-1]=="L":
      rl.append("R")
  elif S[i]!=S[i-1]:
    if rl[i-1]=="R":
      rl.append("R")
    elif rl[i-1]=="L":
      rl.append("L")

if rl.count("L")!=N or S[0]=="W" or S[2*N-1]=="W":
  print(0)
  exit()

ans=1
for i in range(2*N):
  if rl[i]=="R":
    if rl[:i].count("L")-rl[:i].count("R")<0:
      print(0)
      exit()
    else:
      ans*=(rl[:i].count("L")-rl[:i].count("R"))%mod

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

Submission status: Time Limit Exceeded

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

    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 %= (10**9+7)
    if stack!=0 or c==0:
        print(0)
    else:
        for i in range(1, n+1):
            c*=i
        print(c%(10**9+7))

if __name__ == "__main__":
    f_c()

Submission status: Time Limit Exceeded

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

    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
    if stack!=0 or c==0:
        print(0)
    else:
        c %= (10**9+7)
        for i in range(1, n+1):
            c*=i
        print(c%(10**9+7))

if __name__ == "__main__":
    f_c()

Submission status: Time Limit Exceeded

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

    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
    if stack!=0 or c==0:
        print(0)
    else:
        for i in range(1, n+1):
            c*=i
        print(c%(10**9+7))

if __name__ == "__main__":
    f_c()

Submission status: Time Limit Exceeded

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

    stack, c = 1, 1
    if s[0]=="W":
        print(0)
        return 0
    for i in range(1, n*2):
        if s[i]=="B":
            if stack%2==0:
                stack += 1
            else:
                c *= stack
                stack -= 1
        else:
            if stack%2==0:
                c *= stack
                stack -= 1
            else:
                stack += 1
        if stack<0:
            break
    if stack!=0:
        print(0)
    else:
        for i in range(1, n+1):
            c*=i
        print(c%(10**9+7))

if __name__ == "__main__":
    f_c()

Submission status: Time Limit Exceeded

Solution code:
def f_c():
    import math
    n = int(input())
    s = input()

    stack, c = 1, 1
    for i in range(1, n*2):
        if s[i]=="B":
            if stack%2==0:
                stack += 1
            else:
                c *= stack
                stack -= 1
        else:
            if stack%2==0:
                c *= stack
                stack -= 1
            else:
                stack += 1
        if stack<0:
            break
    o = 0 if stack!=0 else c*math.factorial(n)
    print(o%(10**9+7))

if __name__ == "__main__":
    f_c()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
 
MOD = 10**9+7
 
N = int(input())
S = list(input())
 
if S[0] == "W" or S[-2] == "W":
    print(0)
    sys.exit()
 
S = [0 if i == "W" else 1 for i in S]
 
LR = [0 for i in range(2*N)]
count0 = 1
count0_raw = 1
ans = 1
for i in range(1, 2*N):
    if LR[i-1] == 0:
        if S[i] == S[i-1]:
            LR[i] = 1
        else:
            LR[i] = 0
    else:
        if S[i] == S[i-1]:
            LR[i] = 0
        else:
            LR[i] = 1
 
    if LR[i] == 0:
        count0 += 1
        count0_raw += 1
    else:
        ans *= count0 % MOD
        count0 -= 1
 
if count0_raw != N:
    print(0)
    sys.exit()
 
N_k = 1
for i in range(N):
    N_k = N_k*(i+1)%MOD 
 
print(ans*N_k%MOD)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9+7

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

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

S = [0 if i == "W" else 1 for i in S]

LR = [0 for i in range(2*N)]
count0 = 1
count0_raw = 1
ans = 1
for i in range(1, 2*N):
    if LR[i-1] == 0:
        if S[i] == S[i-1]:
            LR[i] = 1
        else:
            LR[i] = 0
    else:
        if S[i] == S[i-1]:
            LR[i] = 0
        else:
            LR[i] = 1

    if LR[i] == 0:
        count0 += 1
        count0_raw += 1
    else:
        ans *= count0 % MOD
        count0 -= 1

if count0_raw != N:
    print(0)
    sys.exit()

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

print(ans*N_k%MOD)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9+7

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

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

S = [0 if i == "W" else 1 for i in S]

LR = [0 for i in range(2*N)]
count0 = 1
count0_raw = 1
ans = 1
for i in range(1, 2*N):
    if LR[i-1] == 0:
        if S[i] == S[i-1]:
            LR[i] = 1
        else:
            LR[i] = 0
    else:
        if S[i] == S[i-1]:
            LR[i] = 0
        else:
            LR[i] = 1

    if LR[i] == 0:
        count0 += 1
        count0_raw += 1
    else:
        ans *= count0 % MOD
        count0 -= 1

if count0_raw != N:
    print(0)
    sys.exit()

def factorial(N, mod):
    if N == 0 or N == 1:
        return 1
    else:
        return N * factorial(N-1, mod) % mod

print(ans*factorial(N,MOD)%MOD)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9+7

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

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

LR = [0 for i in range(2*N)]
count0 = 1
count0_raw = 1
ans = 1
for i in range(1, 2*N):
    if LR[i-1] == 0:
        if S[i] == S[i-1]:
            LR[i] = 1
        else:
            LR[i] = 0
    else:
        if S[i] == S[i-1]:
            LR[i] = 0
        else:
            LR[i] = 1

    if LR[i] == 0:
        count0 += 1
        count0_raw += 1
    else:
        ans *= count0 % MOD
        count0 -= 1

if count0_raw != N:
    print(0)
    sys.exit()

def factorial(N, mod):
    if N == 0 or N == 1:
        return 1
    else:
        return N * factorial(N-1, mod) % mod

print(ans*factorial(N,MOD)%MOD)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9+7

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

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

LR = [0 for i in range(2*N)]
count0 = 1
ans = 1
for i in range(1, 2*N):
    if LR[i-1] == 0:
        if S[i] == S[i-1]:
            LR[i] = 1
        else:
            LR[i] = 0
    else:
        if S[i] == S[i-1]:
            LR[i] = 0
        else:
            LR[i] = 1

    if LR[i] == 0:
        count0 += 1
    else:
        ans *= count0 % MOD
        count0 -= 1

if LR.count(0) != N:
    print(0)
    sys.exit()

def factorial(N, mod):
    if N == 0 or N == 1:
        return 1
    else:
        return N * factorial(N-1, mod) % mod

print(ans*factorial(N,MOD)%MOD)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9+7

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

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

LR = [0 for i in range(2*N)]
for i in range(1, 2*N):
    if LR[i-1] == 0:
        if S[i] == S[i-1]:
            LR[i] = 1
        else:
            LR[i] = 0
    else:
        if S[i] == S[i-1]:
            LR[i] = 0
        else:
            LR[i] = 1

if LR.count(0) != N:
    print(0)
    sys.exit()

count0 = 0
ans = 1
for i in range(2*N):
    if LR[i] == 0:
        count0 += 1
    else:
        ans *= count0 % MOD
        count0 -= 1

def factorial(N, mod):
    if N == 0 or N == 1:
        return 1
    else:
        return N * factorial(N-1, mod) % mod

print(ans*factorial(N,MOD)%MOD)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9+7

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

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

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

if LR.count(0) != N:
    print(0)
    sys.exit()

count0 = 0
ans = 1
for i in range(2*N):
    if LR[i] == 0:
        count0 += 1
    else:
        ans *= count0
        count0 -= 1

def factorial(N, mod):
    if N == 0 or N == 1:
        return 1
    else:
        return N * factorial(N-1, mod) % mod

print(ans*factorial(N,MOD)%MOD)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10**9+7

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

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

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

if LR.count(0) != N:
    print(0)
    sys.exit()

count0 = 0
ans = 1
for i in range(2*N):
    if LR[i] == 0:
        count0 += 1
    else:
        ans *= count0
        count0 -= 1

def factorial(N, mod):
    if N == 0 or N == 1:
        return 1
    else:
        return N * factorial(N, mod) % mod

print(ans*factorial(N,MOD)%MOD)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import math
MOD = 10**9+7

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

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

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

if LR.count(0) != N:
    print(0)
    sys.exit()

count0 = 0
ans = 1
for i in range(2*N):
    if LR[i] == 0:
        count0 += 1
    else:
        ans *= count0
        count0 -= 1

print(ans*math.factorial(N)%MOD)

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
s = input()
count = 0
ans = 1 
for i, x in enumerate(s):
    if (i%2 == 0 and x == "B") or (i%2 == 1 and x == "W"): # B:1(R), W:0(L)
        count += 1
    else:
        ans *= count
        count -= 1
ans *= math.factorial(n)
if count != 0:
    print(0)
else:
    print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
sL = [] # B:1(R), W:0(L)
for i, x in enumerate(s):
    if i%2 == 0:
        if x == "W":
            sL.append(1)
        else:
            sL.append(0)
    if i%2 == 1:
        if x == "B":
            sL.append(1)
        else:
            sL.append(0)

count = 0
ans = 1
for i, x in enumerate(sL):
    if x==0:
        count += 1
    else:
        ans *= count
        count -= 1
for i in range(1, n+1):
    ans *= i     
if count != 0:
    print(0)
else:
    print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
s = list(input())
sL = [] # B:1(R), W:0(L)
for i, x in enumerate(s):
    if i%2 == 0:
        if x == "W":
            sL.append(1)
        else:
            sL.append(0)
    if i%2 == 1:
        if x == "B":
            sL.append(1)
        else:
            sL.append(0)

count = 0
ans = 1
for i, x in enumerate(sL):
    if x==0:
        count += 1
    else:
        ans *= count
        count -= 1
ans *= math.factorial(n)
if count != 0:
    print(0)
else:
    print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import sys
from sys import stdin

def main():
 
    n = int(stdin.readline().rstrip())
    s = stdin.readline().rstrip()
    MOD=10**9+7
    perm=1
    for i in range(1,n+1):
        perm=perm*i%MOD
    ans=1

    dp=1#L
    l=1
    r=0
    for i in range(1,2*n):
        if s[i]==s[i-1]:
            dp=1-dp
        if dp==1:
            l+=1
        else:
            r+=1
            ans*=l-r+1
    if s[0]=='W' or s[-1]=='W' or l!=r:
        print(0)
        sys.exit()
    #print(dp)
    
    
    print(ans*perm%MOD)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
    n=int(input())
    s=input()
    MOD=10**9+7
    perm=1
    for i in range(1,n+1):
        perm=perm*i%MOD
    ans=1
    dp=[-1]*(2*n)

    dp[0]=1#L
    for i in range(1,2*n):
        if s[i]==s[i-1]:
            dp[i]=1-dp[i-1]
        else:
            dp[i]=dp[i-1]
    if s[0]=='W' or s[-1]=='W' or sum(dp)!=n:
        print(0)
        sys.exit()
    #print(dp)
    count=0
    for i in range(2*n):
        if dp[i]==1:
            count+=1
        else:
            ans*=count
            count-=1
    print(ans*perm%MOD)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from math import factorial

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

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

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

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

ct_L, ans = 0, 1

for i in op:
    if i == 'L':
        ct_L += 1
    else:
        ans *= ct_L
        ct_L -= 1

mod = 10**9+7
print(factorial(N) * ans % mod)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
from math import factorial

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

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

op = "L"
ct_L = 1
ans = 1
for i in range(1, 2*N):
    if S[i] == S[i-1]:
        if op[i-1] == 'L':
            op += 'R'
            ans *= ct_L
            ct_L -= 1
        else:
            op += 'L'
            ct_L += 1
    else:
        if op[i-1] == 'L':
            op += 'L'
            ct_L += 1
        else:
            op += 'R'
            ans *= ct_L
            ct_L -= 1

ct_L, ct_R = Counter(op).values()
if ct_L != ct_R:
    print(0)
    sys.exit()

mod = 10**9+7
print(factorial(N) * ans % mod)

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())
s = list(input())
b = [0]
if s[0] == "W" or s[-1] == "W": print(0)
else:
  for i in range(1,2*n):
    if s[i] != s[i-1]:
  	  b.append(b[-1])
    else:
      b.append(b[-1]^1)
  if b[-1] == 0: print(0)
  elif sum(b) != n: print(0)
  else:
    res = 1
    num = [0,0]
    for eachb in b:
      if eachb == 0:
        num[0] += 1
      else:
        res *= num[0] - num[1]
        num[1] += 1
    print((res * math.factorial(n)) % (10**9 + 7))