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: Time Limit Exceeded

Solution code:
import math
n = int(input())
s = list(input())
#l = [1 if eachs=="B" else 0 for eachs in s]
# left:0 right:1
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))

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 = [0]*(2*N)
op[0] = False
ct_L = 1
ans = 1
for i in range(1, 2*N):
    if S[i] == S[i-1]:
        if op[i-1] is False:
            op[i] = True
            ans *= ct_L
            ct_L -= 1
        else:
            op[i] = False
            ct_L += 1
    else:
        if op[i-1] is False:
            op[i] = False
            ct_L += 1
        else:
            op[i] = True
            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 sys
from collections import Counter


def factorial(N: int, mod: int) -> int:
    res = 1
    for i in range(1, N+1):
        res *= i
        res %= mod
    return res


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

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

op = [0]*(2*N)
op[0] = False
ct_L = 1
ans = 1
for i in range(1, 2*N):
    if S[i] == S[i-1]:
        if op[i-1] is False:
            op[i] = True
            ans *= ct_L
            ct_L -= 1
        else:
            op[i] = False
            ct_L += 1
    else:
        if op[i-1] is False:
            op[i] = False
            ct_L += 1
        else:
            op[i] = True
            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, mod) * ans % mod)

Submission status: Time Limit Exceeded

Solution code:
import sys


def factorial(N: int, mod: int) -> int:
    res = 1
    for i in range(1, N+1):
        res *= i
        res %= mod
    return res


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

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

op = [0]*(2*N)
op[0] = False
ct_L = 1
ans = 1
for i in range(1, 2*N):
    if S[i] == S[i-1]:
        if op[i-1] is False:
            op[i] = True
            ans *= ct_L
            ct_L -= 1
        else:
            op[i] = False
            ct_L += 1
    else:
        if op[i-1] is False:
            op[i] = False
            ct_L += 1
        else:
            op[i] = True
            ans *= ct_L
            ct_L -= 1

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys


def factorial(N: int, mod: int) -> int:
    res = 1
    for i in range(1, N+1):
        res *= i
        res %= mod
    return res


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

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

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

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

ans = 1
L_ct = 0
R_ct = 0
for i in op:
    if i:
        ans *= L_ct
        L_ct -= 1
    else:
        L_ct += 1
mod = 10**9+7
print(factorial(N, mod) * ans % mod)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
N=int(input())
S=input()
D=[0]*2*N
D[0]=0
D[-1]=1
MOD=10**9+7

if S[0]=='W' or S[-1]=='W':
    print(0)
    sys.exit()
    
for i in range(1,2*N-1):
    if S[i-1]==S[i]:
        D[i]=(D[i-1]+1)%2
    else:
        D[i]=D[i-1]

if sum(D)!=N:
    print(0)
    sys.exit()
 
cnt=0
ans=1
for i in range(2*N):
    if D[i]==1:
        ans*=i-2*cnt
    cnt+=D[i]

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

Submission status: Time Limit Exceeded

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


class FactMod():
    '''
    modの値が素数の時のfactと組み合わせを求める
    フェルマーの小定理を用いているため、modが素数以外の時は使えない
    '''

    def __init__(self, n, mod):
        '''
        コンストラクタ
        f:nまでの i!の値を　配列に入れる
        inv: (i!)^-1　の値を配列に入れる
        '''
        self.mod = mod
        self.f = [1]*(n+1)
        for i in range(1, n+1):
            self.f[i] = self.f[i-1]*i % mod

        self.inv = [pow(self.f[-1], mod-2, mod)]
        for i in range(1, n+1)[::-1]:
            self.inv.append(self.inv[-1]*i % mod)
        self.inv.reverse()

    def fact(self, n):
        '''
        n!の値を返す
        '''
        return self.f[n]

    def comb(self, n, r):
        '''
        nCrの値を返す
        '''
        ret = self.f[n] * self.inv[n-r]*self.inv[r]
        ret %= self.mod
        return ret

    def perm(self, n, r):
        """
        nPrの値を返す
        """
        ret = self.f[n] * self.inv[n-r]
        ret %= self.mod
        return ret

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


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

L = [0]*(2*N)
for i, c in enumerate(S):
    if c == "B":
        L[i] = 1
    if i % 2 == 0:
        L[i] ^= 1
if sum(L) != N:
    ans = 0
else:
    ans = 1
    cnt = 0
    for l in L:
        if l == 0:
            cnt += 1
        else:
            ans *= cnt
            cnt -= 1
ans *= F.fact(N)
ans %= MOD
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
MOD = 1000000007


def solve(S, N):
    a = np.zeros(2*N, dtype=np.int64)
    if S[0] == 87:
        return 0
    a[0] = 1
    for i in range(1, 2 * N):
        # L - R の回数だけひっくり返される
        if i & 1:
            if S[i] == 66:
                a[i] = a[i - 1]
                if 2 * a[i] < i + 1:
                    return 0
            else:
                a[i] = a[i - 1] + 1
        else:
            if S[i] == 87:
                a[i] = a[i - 1]
                if 2 * a[i] < i + 1:
                    return 0
            else:
                a[i] = a[i - 1] + 1
    if a[2 * N - 1] > N:
        return 0

    ans = 1
    for n in range(1, 2*N):
        if a[n] == a[n - 1]:
            ans = ans * (2 * a[n] - n) % MOD
    for n in range(2, N + 1):
        ans = ans * n % MOD
    return ans


stdin = open(0, 'rb')
N = int(stdin.readline())
S = np.frombuffer(stdin.readline(), dtype=np.int8)
# B 66 W 87
print(solve(S, N))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import *
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    m, d = LI()
    ans = 0
    for i in range(1, m + 1):
        for j in range(10, d + 1):
            if int(str(j)[0]) < 2 or int(str(j)[1]) < 2:
                continue
            if i == int(str(j)[0]) * int(str(j)[1]):
                ans += 1
    print(ans)
    return

#B
def B():
    n, l = LI()
    a = LI()
    d0 = defaultdict(int)
    d1 = defaultdict(int)
    for i in a:
        d0[i] += 1
    for i in range(n):
        b = 0
        for k in range(i+1, n):
            if a[i] > a[k]:
                b += 1
        d1[i] = b
    ans = 0
    d2 = list(d0.items())
    d2.sort()
    d3 = defaultdict(int)
    d = 0
    for key, value in d2:
        d3[key] = d
        d += value
    for i in range(n):
        ans += d3[a[i]] * l * (l-1) // 2 + d1[i] * l
        ans %= mod
    print(ans % mod)
    return

#C
def C():
    n = II()
    s = S()
    lis = [0]
    for i in range(2 * n - 1):
        lis.append(lis[-1] ^ (s[i] == s[i+1]))
    l = [0] * (2 * n)
    r = [0] * (2 * n)
    for i in range(2 * n):
        l[i] += l[i - 1] + (lis[i] == 0)
        r[i] += r[i - 1] + (lis[i] == 1)
    ans = 1
    if l[-1] != r[-1] or s[0] == "W":
        print(0)
        return
    for i in range(2 * n):
        if lis[i]:
            ans *= l[i] - (r[i] - 1) % mod
    print(ans * math.factorial(n) % mod)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
n = int(input())
s = list(input())
def count_combination(x):
    if sum(x) != n:
        return(0)
    total_count = 1
    false_count = 0
    for i in range(2*n):
        if x[i]:
            total_count *= false_count
            false_count -= 1
        else:
            false_count += 1
        if false_count == -1:
            return(0)
    return(total_count)
sb = list(map(lambda x: False if x == 'W' else True, s))
sb[::2] = list(map(lambda x: not x, sb[::2]))

total_count = count_combination(sb)

permutation = reduce(lambda x, y: x*y%(10**9+7), range(1,n+1))

print(total_count * permutation % (10**9+7))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
n = int(input())
s = list(input())
def count_combination(x):
    if sum(x) != n:
        return(0)
    total_count = 1
    false_count = 0
    for i in range(2*n):
        if x[i]:
            total_count *= false_count
            false_count -= 1
        else:
            false_count += 1
        if false_count == -1:
            return(0)
    return(total_count)
sb = list(map(lambda x: False if x == 'W' else True, s))
sb[::2] = list(map(lambda x: not x, sb[::2]))

total_count = count_combination(sb)

permutation = reduce(lambda x, y: x*y, range(1,n+1))

print(total_count * permutation % (10**9+7))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
def count_combination(x):
    if sum(x) != n:
        return(0)
    total_count = 1
    false_count = 0
    for i in range(2*n):
        if x[i]:
            total_count *= false_count
            false_count -= 1
        else:
            false_count += 1
        if false_count == -1:
            return(0)
    return(total_count)
sb = list(map(lambda x: False if x == 'W' else True, s))
sb[::2] = list(map(lambda x: not x, sb[::2]))

total_count = count_combination(sb)

permutation = 1
for _ in range(n):
    permutation = permutation * (_ + 1) % (10**9+7)

print(total_count * permutation % (10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
s = input()
s2 = list(s)

if s[0] == 'B':
    s2[0] = 'L'
else:
    s2[0] = 'R'

for i in range(1,2*n): 
    if s[i-1] == s[i]:
        if s2[i-1] == 'L':
            s2[i] = 'R'
        else:
            s2[i] = 'L'
    else:
        if s2[i-1] == 'L':
            s2[i] = 'L'
        else:
            s2[i] = 'R'
        
if s2[0] == 'R' or s2[2*n-1] == 'L' or s2.count('L') != s2.count('R'):
    print(0)
    exit()
            
ans = 1
count_l = 0
for s2_str in s2: 
    if s2_str == 'L':
        count_l += 1
    else:
        ans *= count_l
        count_l -= 1
    
print((ans * math.factorial(n)) % (10**9 + 7))

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
s = input()
s2 = list(s)

if s[0] == 'B':
    s2[0] = 'L'
else:
    s2[0] = 'R'

for i in range(1,2*n): 
    if s[i-1] == s[i]:
        if s2[i-1] == 'L':
            s2[i] = 'R'
        else:
            s2[i] = 'L'
    else:
        if s2[i-1] == 'L':
            s2[i] = 'L'
        else:
            s2[i] = 'R'
        
if s2[0] == 'R' or s2[2*n-1] == 'L' or s2.count('L') != s2.count('R'):
    print(0)
    exit()
            
ans = 1
count_l = 0
for i in range(2*n): 
    if s2[i] == 'L':
        count_l += 1
    else:
        ans *= count_l % (10**9 + 7)
        count_l -= 1
    
print((ans * math.factorial(n)) % (10**9 + 7))

Submission status: Time Limit Exceeded

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

N = int(input())
S = list(map(lambda x: int(x == "W"),input()))
if S[0] or S[-1]:
    print(0)
    exit(0)

Dc = 1
LRsub = 1
Pattern = 1
Sp = S[0]

for i in range(1,N*2):
    Sc = S[i]
    Dc = Dc if Sc != Sp else -Dc
    Sp = Sc
    if Dc < 0:
        Pattern *= LRsub
    LRsub += Dc

if LRsub == 0:
    MOD = 10 ** 9 + 7
    for i in range(2,N+1):
        Pattern *= i
        Pattern = Pattern % MOD
    print(Pattern)
else:
    print(0)

Submission status: Time Limit Exceeded

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

N = int(input())
S = input()
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit(0)

Dc = 1
LRsub = 1
Pattern = 1
Sp = S[0]

for i, Sc in enumerate(S):
    if i == 0:
        continue
    if Sc != 'B' and Sc != 'W':
        continue
    Dc = Dc if Sc != Sp else -Dc
    Sp = Sc
    if Dc < 0:
        Pattern *= LRsub
    LRsub += Dc

if LRsub == 0:
    MOD = 10 ** 9 + 7
    for i in range(2,N+1):
        Pattern *= i
        Pattern = Pattern % MOD
    print(Pattern)
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
s = input()
s2 = list(s)

if s[0] == 'B':
    s2[0] = 'L'
else:
    s2[0] = 'R'

for i in range(1,2*n): 
    if s[i-1] == s[i]:
        if s2[i-1] == 'L':
            s2[i] = 'R'
        else:
            s2[i] = 'L'
    else:
        if s2[i-1] == 'L':
            s2[i] = 'L'
        else:
            s2[i] = 'R'
        
if s2[0] == 'R' or s2[2*n-1] == 'L' or s2.count('L') != s2.count('R'):
    print(0)
    exit()
            
ans = 1
count_l = 0
for i in range(0,2*n): 
    if s2[i] == 'L':
        count_l += 1
    else:
        ans *= count_l
        count_l -= 1
    
print((ans * math.factorial(n)) % (10**9 + 7))

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
s = input()
s2 = list(s)

count_l = 0
count_r = 0

if s[0] == 'B':
    s2[0] = 'L'
    count_l += 1
else:
    s2[0] = 'R'
    count_r += 1

for i in range(1,2*n): 
    if s[i-1] == s[i]:
        if s2[i-1] == 'L':
            s2[i] = 'R'
            count_r += 1
        else:
            s2[i] = 'L'
            count_l += 1
    else:
        if s2[i-1] == 'L':
            s2[i] = 'L'
            count_l += 1
        else:
            s2[i] = 'R'
            count_r += 1
        
if s2[0] == 'R' or s2[2*n-1] == 'L' or count_l != count_r:
    print(0)
    exit()
            
ans = 1
count_l = 0
for i in range(0,2*n): 
    if s2[i] == 'L':
        count_l += 1
    else:
        ans *= count_l
        count_l -= 1
    
print((ans * math.factorial(n)) % (10**9 + 7))

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
s = input()
s2 = list(s)

if s[0] == 'B':
    s2[0] = 'L'
else:
    s2[0] = 'R'

for i in range(1,2*n): 
    if s[i-1] == s[i]:
        if s2[i-1] == 'L':
            s2[i] = 'R'
        else:
            s2[i] = 'L'
    else:
        if s2[i-1] == 'L':
            s2[i] = 'L'
        else:
            s2[i] = 'R'

count_l = 0
count_r = 0
for i in range(2*n): 
    if s2[i] == 'L':
        count_l += 1
    else:
        count_r += 1
        
if s2[0] == 'R' or s2[2*n-1] == 'L' or count_l != count_r:
    print(0)
    exit()
            
ans = 1
count_l = 0
for i in range(0,2*n): 
    if s2[i] == 'L':
        count_l += 1
    else:
        ans *= count_l
        count_l -= 1
    
print((ans * math.factorial(n)) % (10**9 + 7))

Submission status: Time Limit Exceeded

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


MOD = 10 ** 9 + 7

input = sys.stdin.readline
N = int(input())
S = input()
if S[0] == 'W' or S[-1] == 'W':
    print(0)
    exit(0)

Dc = 1
LRsub = 1
Pattern = 1
for i in range(1,2*N):
    Dc = Dc if S[i] != S[i-1] else -Dc
    if Dc < 0:
        Pattern *= LRsub
    LRsub += Dc

if LRsub == 0:
    for i in range(2,N+1):
        Pattern *= i
        Pattern = Pattern % MOD
    print(Pattern)
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
import sys


MOD = 10 ** 9 + 7

def main():
    input = sys.stdin.readline
    N = int(input())
    S = input()
    if S[0] == 'W' or S[-1] == 'W':
        print(0)
        return

    Dc = 1
    LRsub = 1
    Pattern = 1
    for i in range(1,2*N):
        Dc = Dc if S[i] != S[i-1] else -Dc
        if Dc < 0:
           Pattern *= LRsub
        LRsub += Dc

    if LRsub == 0:
        for i in range(2,N+1):
            Pattern *= i
            Pattern = Pattern % MOD
        print(Pattern)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
import sys


MOD = 10 ** 9 + 7

def main():
    input = sys.stdin.readline
    N = int(input())
    S = input()
    if S[0] == 'W' or S[-1] == 'W':
        print(0)
        return

    Dc = 1
    LRsub = 1
    Pattern = 1
    for i in range(1,2*N):
        Dc = Dc if S[i] != S[i-1] else -Dc
        if Dc < 0:
           Pattern *= LRsub
        LRsub += Dc

    if LRsub == 0:
        Pattern *= math.factorial(N)
        Pattern = Pattern % MOD
        print(Pattern)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

MOD = 10 ** 9 + 7

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

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

    Dc = 1
    LRsub = 1
    Pattern = 1
    for i in range(1,2*N):
        Dc = Dc if S[i] != S[i-1] else -Dc
        if Dc < 0:
           Pattern *= LRsub
        LRsub += Dc

    if LRsub == 0:
        Pattern *= math.factorial(N)
        Pattern = Pattern % MOD
        print(Pattern)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

MOD = 10 ** 9 + 7

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

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

    Dc = 1
    LRsub = 1
    Pattern = 1
    for i in range(1,2*N):
        Dc = Dc if S[i] != S[i-1] else -Dc
        Pattern *= LRsub if Dc < 0 else 1
        LRsub += Dc

    if LRsub == 0:
        Pattern *= math.factorial(N)
        Pattern = Pattern % MOD
        print(Pattern)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

MOD = 10 ** 9 + 7

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

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

    Dc = True
    LRsub = 1
    Pattern = 1
    for i in range(1,2*N):
        Dc = Dc if S[i] != S[i-1] else not Dc
        Pattern *= LRsub if not Dc else 1
        LRsub += 1 if Dc else -1

    if LRsub == 0:
        Pattern *= math.factorial(N)
        Pattern = Pattern % MOD
        print(Pattern)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

MOD = 10 ** 9 + 7

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

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

    Sp = ''
    Dc = True
    LRsub = 0
    Pattern = 1
    for Sc in S:
        Dc = not Dc if Sc == Sp else Dc
        Sp = Sc
        Pattern *= LRsub if not Dc else 1
        LRsub += 1 if Dc else -1

    if LRsub == 0:
        Pattern *= math.factorial(N)
        Pattern = Pattern % MOD
        print(Pattern)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

MOD = 10 ** 9 + 7

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

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

    Sp = ''
    Dc = True
    Lcnt = 0
    Rcnt = 0
    Pattern = 1
    for Sc in S:
        Dc = not Dc if Sc == Sp else Dc
        Sp = Sc
        Pattern *= Lcnt - Rcnt if not Dc else 1
        Lcnt += 1 if Dc else 0
        Rcnt += 1 if not Dc else 0

    if Lcnt == Rcnt:
        Pattern *= math.factorial(N)
        Pattern = Pattern % MOD
        print(Pattern)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

MOD = 10 ** 9 + 7

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

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

    Sp = ''
    Dc = True
    Lcnt = 0
    Rcnt = 0
    Pattern = 1
    for Sc in S:
        if Sc == Sp:
            Dc = not Dc
        Sp = Sc

        if not Dc:
            Pattern *= Lcnt - Rcnt

        Lcnt += 1 if Dc else 0
        Rcnt += 1 if not Dc else 0

    if Lcnt == Rcnt:
        Pattern *= math.factorial(N)
        Pattern = Pattern % MOD
        print(Pattern)
    else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import factorial as fc
N = int(input())
S = input()

L = ["L"]
if S[0] == "W": L[0] = "R" 
for i in range(1, 2 * N):
  if S[i] == S[i-1]:
    if L[i-1] == "L":
      L.append("R")
    else:
      L.append("L")
  else:
    if L[i-1] == "L":
      L.append("L")
    else:
      L.append("R")
if L[0] == "R" or L.count("L") != L.count("R"):
  print (0)
else:
  ans = 1
  for i in range(2*N):
    if L[i] == "R":
      ans *= L[:i].count("L") - L[:i].count("R")
  print (ans * fc(N))

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n = int(input())
    s_list = list(input())
    mod = 10 ** 9 + 7

    if s_list[0] == "W" or s_list[-1] == "W":
        print(0)
        return

    from math import factorial

    l_counter = 0
    res = 1

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

        if s == "W":
            l_counter += 1
        else:
            res *= l_counter
            l_counter -= 1
            if l_counter < 0:
                print(0)
                return

    if l_counter != 0:
        print(0)
    else:
        print(res * factorial(n) % mod)
        
resolve()

Submission status: Time Limit Exceeded

Solution code:
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


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

print(answer)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n = int(input())
    s_list = list(input())
    mod = 10 ** 9 + 7

    from math import factorial

    l_counter = 0
    res = 1

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

        if s == "W":
            l_counter += 1
        else:
            res *= l_counter
            l_counter -= 1
            if l_counter < 0:
                print(0)
                return

    if l_counter != 0:
        print(0)
    else:
        print(res * factorial(n) % mod)
        
resolve()

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
s = list(input())
mod = 10**9+7

cnt_w = 0
cnt_ans = 1

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

if s.count("B") != s.count("W"):
    print(0)
    exit()
    
print((cnt_ans*math.factorial(n))%mod)

Submission status: Time Limit Exceeded

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

from math import factorial

l_counter = 0
pair_counter = 0
res = 1

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

    if s == "W":
        l_counter += 1
    else:
        pair_counter += 1
        res *= l_counter
        l_counter -= 1

if pair_counter != n:
    print(0)
else:
    print(res * factorial(n) % mod)

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
s = [x for x in input()]
mod = 10**9+7

for i in range(0,n*2,2):
    if s[i] == "B":
        s[i] = "W"
    else:
        s[i] = "B"

cnt_w = 0
cnt_ans = 1

if s.count("B") != s.count("W"):
    print(0)
    exit()
    
for i in range(n*2):
    if s[i] == "W":
        cnt_w += 1
        continue
    if s[i] == "B":
        cnt_ans *= cnt_w
        cnt_w -= 1
    
print((cnt_ans*math.factorial(n))%mod)

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
s = [x for x in input()]
mod = 10**9+7

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

cnt_w = 0
cnt_ans = 1

if s.count("B") != s.count("W"):
    print(0)
    exit()
    
for i in range(n*2):
    if s[i] == "W":
        cnt_w += 1
        continue
    if s[i] == "B":
        cnt_ans *= cnt_w
        cnt_w -= 1
    
print((cnt_ans*math.factorial(n))%mod)

Submission status: Time Limit Exceeded

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

from math import factorial

for i in range(n):
    s_list[i * 2] = "B" if s_list[i * 2] == "W" else "W"

l_counter = 0
pair_counter = 0
res = 1
for s in s_list:
    if s == "W":
        l_counter += 1
    else:
        pair_counter += 1
        res *= l_counter
        l_counter -= 1

if pair_counter != n:
    print(0)
else:
    print(res * factorial(n) % mod)

Submission status: Time Limit Exceeded

Solution code:

M = 10**9+7
N = int(input())
S = list(input())
s = []

#0ならW、1ならB
s_append = s.append
for i in range(2*N):
    if S[i] == "B":
        s_append(1)
    else:
        s_append(0)

#端が0なら0を返す
if s[0] == 0 or s[2*N-1] == 0:
    print(0)
    exit()

ans = 1
cnt = 1
lis = [0] #0なら左、1なら右
#全て0にする
lis_append = lis.append
for j in range(1,2*N):
    j1 = lis[j-1]
    if s[j-1] != s[j]:
        lis_append(j1)
        if j1 ==0:
            cnt+=1
        else:
            ans *= cnt
            cnt -= 1
    elif j1 == 0:
        lis_append(1)
        ans *= cnt
        cnt -= 1
    else:
        lis_append(0)
        cnt += 1

#左と右の数が違ったら0を返す
if lis.count(0) != lis.count(1):
    print(0)
    exit()

#N!をかける
for l in range(1,N+1):
    ans *= l
    ans %= M
print(ans)

Submission status: Time Limit Exceeded

Solution code:

M = 10**9+7
N = int(input())
S = list(input())
s = []

#0ならW、1ならB
s_append = s.append
for i in range(2*N):
    if S[i] == "B":
        s_append(1)
    else:
        s_append(0)

#端が0なら0を返す
if s[0] == 0 or s[2*N-1] == 0:
    print(0)
    exit()

ans = 0
lis = [0] #0なら左、1なら右
#全て0にする
lis_append = lis.append
for j in range(1,2*N):
    j1 = lis[j-1]
    if s[j-1] != s[j]:
        lis_append(j1)
    elif j1 == 0:
        lis_append(1)
    else:
        lis_append(0)

#左と右の数が違ったら0を返す
if lis.count(0) != lis.count(1):
    print(0)
    exit()

#"右"が選べる"左"の選択肢を掛けていく
cnt = 1
ans = 1
for k in range(1,2*N):
    if lis[k] == 0:
        cnt += 1
    else:
        ans *= cnt
        cnt -= 1

#N!をかける
for l in range(1,N+1):
    ans *= l
    ans %= M
print(ans)

Submission status: Time Limit Exceeded

Solution code:

M = 10**9+7
N = int(input())
S = list(input())
s = []

#0ならW、1ならB
for i in range(2*N):
    if S[i] == "B":
        s.append(1)
    else:
        s.append(0)

#端が0なら0を返す
if s[0] == 0 or s[2*N-1] == 0:
    print(0)
    exit()

ans = 0
lis = [0] #0なら左、1なら右
#全て0にする
lis_append = lis.append
for j in range(1,2*N):
    j1 = lis[j-1]
    if s[j-1] != s[j]:
        lis_append(j1)
    elif j1 == 0:
        lis_append(1)
    else:
        lis_append(0)

#左と右の数が違ったら0を返す
if lis.count(0) != lis.count(1):
    print(0)
    exit()

#"右"が選べる"左"の選択肢を掛けていく
cnt = 1
ans = 1
for k in range(1,2*N):
    if lis[k] == 0:
        cnt += 1
    else:
        ans *= cnt
        cnt -= 1

#N!をかける
for l in range(1,N+1):
    ans *= l
    ans %= M
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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