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:
#!/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)
    for i in range(2 * n):
        l[i] += l[i - 1] + (lis[i] == 0)
    ans = 1
    if l[-1] != n or s[0] == "W":
        print(0)
        return
    k = 0
    for i in range(2 * n):
        if lis[i]:
            ans *= l[i] - (k - 1)
            ans %= mod
            k += 1
    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: Wrong Answer

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]:
        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: Wrong Answer

Solution code:
from math import factorial

N = int(input())
d = {"W":0, "B":1}
ls = [d[s] for s in input()]
r_ls = reversed(ls)
xs = []
mod = 10**9 + 7
# 全部左右とか関係なく取ったとする
for i,r in enumerate(r_ls):
    xs.append(r + i)
# 右でトルものを選ぶ(↑の操作後、mod2が1のところ)
LR = [x%2 for x in list(reversed(xs))]

# Lの数を持ちながら、Rが来たら消化＆場合の数にLの数をかける
cnt = 0 # Lの数
num = 1 # 場合の数

# 最後がBじゃないなら、どうやっても0
if LR[-1] == 0:
    num = 0
for lr in LR:
    if lr == 0:
        cnt += 1
    else:
        num *= cnt
        cnt -= 1
        cnt = max(0,cnt)
        num %= mod
ans = num * factorial(N)
print(ans%mod)

Submission status: Wrong Answer

Solution code:
"""
https://atcoder.jp/contests/jsc2019-qual/tasks/jsc2019_qual_c

B -> 1
W -> 0

BWWWWB
100001

反転操作を３つに分解
左端からl-1まで+1
左端からr-1まで+1
rに+1

こうしておくとつまり、右端として選ぶかどうかを決めるだけでよくなる

"""

from math import factorial

N = int(input())
d = {"W":0, "B":1}
ls = [d[s] for s in input()]
r_ls = reversed(ls)
xs = []
mod = 10**9 + 7
# 全部左右とか関係なく取ったとする
for i,r in enumerate(r_ls):
    xs.append(r + i)
# 右でトルものを選ぶ(↑の操作後、mod2が1のところ)
LR = [x%2 for x in list(reversed(xs))]
# print(LR)

# Lの数を持ちながら、Rが来たら消化＆場合の数にLの数をかける
cnt = 0 # Lの数
num = 1 # 場合の数

for lr in LR:
    if lr == 0:
        cnt += 1
    else:
        num *= cnt
        cnt -= 1
        cnt = max(0,cnt)
        num %= mod

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

Submission status: Wrong Answer

Solution code:
"""
https://atcoder.jp/contests/jsc2019-qual/tasks/jsc2019_qual_c

B -> 1
W -> 0

BWWWWB
100001

反転操作を３つに分解
左端からl-1まで+1
左端からr-1まで+1
rに+1

こうしておくとつまり、右端として選ぶかどうかを決めるだけでよくなる

"""

from math import factorial

N = int(input())
d = {"W":0, "B":1}
ls = [d[s] for s in input()]
r_ls = reversed(ls)
xs = []
mod = 10**9 + 7
# 全部左右とか関係なく取ったとする
for i,r in enumerate(r_ls):
    xs.append(r + i)
# 右でトルものを選ぶ(↑の操作後、mod2が1のところ)
LR = [x%2 for x in list(reversed(xs))]
# print(LR)

# Lの数を持ちながら、Rが来たら消化＆場合の数にLの数をかける
cnt = 0 # Lの数
num = 1 # 場合の数

for lr in LR:
    if lr == 0:
        cnt += 1
    else:
        num *= cnt
        cnt -= 1
        num %= mod

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

Submission status: Wrong Answer

Solution code:
"""
https://atcoder.jp/contests/jsc2019-qual/tasks/jsc2019_qual_c

B -> 1
W -> 0

BWWWWB
100001

反転操作を３つに分解
左端からl-1まで+1
左端からr-1まで+1
rに+1

こうしておくとつまり、右端として選ぶかどうかを決めるだけでよくなる

"""

from math import factorial

N = int(input())
d = {"W":0, "B":1}
ls = [d[s] for s in input()]
r_ls = reversed(ls)
xs = []
# 全部左右とか関係なく取ったとする
for i,r in enumerate(r_ls):
    xs.append(r + i)
# 右でトルものを選ぶ(↑の操作後、mod2が1のところ)
LR = [x%2 for x in list(reversed(xs))]
# print(LR)

# Lの数を持ちながら、Rが来たら消化＆場合の数にLの数をかける
cnt = 0 # Lの数
num = 1 # 場合の数

for lr in LR:
    if lr == 0:
        cnt += 1
    else:
        num *= cnt
        cnt -= 1

ans = num * factorial(N)
print(ans)

Submission status: Wrong Answer

Solution code:
from math import factorial

N = int(input())
d = {"W":0, "B":1}
ls = [d[s] for s in input()]
r_ls = reversed(ls)
xs = []
# 全部左右とか関係なく取ったとする
for i,r in enumerate(r_ls):
    xs.append(r + i)
# 右でトルものを選ぶ(↑の操作後、mod2が1のところ)
LR = [x%2 for x in list(reversed(xs))]
# print(LR)

# Lの数を持ちながら、Rが来たら消化＆場合の数にLの数をかける
cnt = 0 # Lの数
num = 1 # 場合の数

for lr in LR:
    if lr == 0:
        cnt += 1
    else:
        num *= cnt
        cnt -= 1

ans = num * factorial(N)
print(ans)

Submission status: Wrong Answer

Solution code:
from math import factorial

N = int(input())
d = {"W":0, "B":1}
ls = [d[s] for s in input()]
r_ls = reversed(ls)
xs = []
# 全部左右とか関係なく取ったとする
for i,r in enumerate(r_ls):
    xs.append(r + i)
# 右でトルものを選ぶ(↑の操作後、mod2が1のところ)
LR = [x%2 for x in list(reversed(xs))]
print(LR)

# Lの数を持ちながら、Rが来たら消化＆場合の数にLの数をかける
cnt = 0 # Lの数
num = 1 # 場合の数

for lr in LR:
    if lr == 0:
        cnt += 1
    else:
        num *= cnt
        cnt -= 1

ans = num * factorial(N)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
M = 10**9 + 7
n = int(input()) * 2
s = list(map(lambda x: int(x == "W"), input()))

lr = [0] * n # L: 0, R: 1

if s[0] or s[-1]:
    print(0)
    sys.exit()

l = 1
ans = 1
for i, v in enumerate(s[1:], 1):
    if v:
        if l % 2:
            l += 1
        else:
            ans *= l
            l -= 1
            lr[i] = 1
    else:
        if l % 2 == 0:
            l += 1
        else:
            ans *= l
            l -= 1
            lr[i] = 1
    if ans > M:
        ans %= M
    if l < 0:
        print(0)
        sys.exit()

if not lr[-1] or l == 0:
    print(0)
    sys.exit()

def fact(n):
    result = 1
    for i in range(2, n + 1):
        result *= i
        if result > M:
            result %= M
    return result
print((ans * fact(n // 2)) % M)

Submission status: Wrong Answer

Solution code:
import sys
M = 10**9 + 7
n = int(input()) * 2
s = list(map(lambda x: int(x == "W"), input()))

lr = [0] * n # L: 0, R: 1

if s[0] or s[-1]:
    print(0)
    sys.exit()

l = 1
ans = 1
for i, v in enumerate(s[1:], 1):
    if v:
        if l % 2:
            l += 1
        else:
            ans *= l
            l -= 1
            lr[i] = 1
    else:
        if l % 2 == 0:
            l += 1
        else:
            ans *= l
            l -= 1
            lr[i] = 1
    if ans > M:
        ans %= M
    if l < 0:
        print(0)
        sys.exit()


if not lr[-1]:
    print(0)
    sys.exit()

def fact(n):
    result = 1
    for i in range(2, n + 1):
        result *= i
        if result > M:
            result %= M
    return result
print((ans * fact(n // 2)) % M)

Submission status: Wrong Answer

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(-2)

Submission status: Wrong Answer

Solution code:
import sys
M = 10**9 + 7
n = int(input()) * 2
s = list(map(lambda x: int(x == "W"), input()))

lr = [0] * n # L: 0, R: 1

if s[0]:
    print(0)
    sys.exit()

l = 1
ans = 1
for i, v in enumerate(s[1:], 1):
    if v:
        if l % 2:
            l += 1
        else:
            ans *= l
            l -= 1
            lr[i] = 1
    else:
        if l % 2 == 0:
            l += 1
        else:
            ans *= l
            l -= 1
            lr[i] = 1
    if ans > M:
        ans %= M
    if l < 0:
        print(0)
        sys.exit()


if not lr[-1]:
    print(0)
    sys.exit()

def fact(n):
    result = 1
    for i in range(2, n + 1):
        result *= i
        if result > M:
            result %= M
    return result
print(ans * fact(n // 2) % M)

Submission status: Wrong Answer

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,1):
    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: Wrong Answer

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))

Submission status: Wrong Answer

Solution code:
import sys
M = 10**9 + 7
n = int(input()) * 2
s = list(map(lambda x: int(x == "W"), input()))

lr = [0] * n # L: 0, R: 1

if s[0]:
    print(0)
    sys.exit()

l = 1
ans = 1
for i, v in enumerate(s[1:], 1):
    if v:
        if l % 2:
            l += 1
        else:
            ans *= l
            l -= 1
            lr[i] = 1
    else:
        if l % 2 == 0:
            l += 1
        else:
            ans *= l
            l -= 1
            lr[i] = 1
    if l < 0:
        print(0)
        sys.exit()


if not lr[-1]:
    print(0)
    sys.exit()

def fact(n):
    result = 1
    for i in range(2, n + 1):
        result *= i
        if result > M:
            result %= M
    return result
print(ans * fact(n // 2))

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [v for v in input()]

# determine L or R
lst = [0 for i in range(2 * n)]
for i in range(2 * n):
    if S[i] == 'W' and (2 * n - i - 1) % 2 == 0:
        lst[i] = 'L'
    elif S[i] == 'W' and (2 * n - i - 1) % 2 == 1:
        lst[i] = 'R'
    elif S[i] == 'B' and (2 * n - i - 1) % 2 == 0:
        lst[i] = 'R'
    elif S[i] == 'B' and (2 * n - i - 1) % 2 == 1:
        lst[i] = 'L'

print(lst)

deq = 0
val = 1
#tracing lst
for i in range(2 * n):
    if lst[i] == 'L':
        deq += 1
    elif lst[i] == 'R':
        val *= deq
        val %= 10 ** 9 + 7
        deq -= 1

if deq != 0:
    print(0)
else:
    for i in range(1, n + 1):
        val = val * i % (10 ** 9 +7)
    print(val)

Submission status: Wrong Answer

Solution code:

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [v for v in input()]

# determine L or R
lst = [0 for i in range(2 * n)]
for i in range(2 * n):
    if S[i] == 'W' and (2 * n - i - 1) % 2 == 0:
        lst[i] = 'L'
    elif S[i] == 'W' and (2 * n - i - 1) % 2 == 1:
        lst[i] = 'R'
    elif S[i] == 'B' and (2 * n - i - 1) % 2 == 0:
        lst[i] = 'R'
    elif S[i] == 'B' and (2 * n - i - 1) % 2 == 1:
        lst[i] = 'L'

deq = 0
val = 1
#tracing lst
for i in range(2 * n):
    if lst[i] == 'L':
        deq += 1
    elif lst[i] == 'R':
        val *= deq
        val %= 10 ** 9 + 7
        deq -= 1

if deq != 0:
    print(-1)
else:
    for i in range(1, n + 1):
        val = val * i % (10 ** 9 +7)
    print(val)

Submission status: Wrong Answer

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

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

lst = [0] * (2 * N + 1)
ans = [-1] * (2 * N + 1) #左で選んだ時1, 右で選んだ時0
lst[0] = 1
ans[0] = 1
for i in range(1, 2 * N):
    if S[i] == 'B': #奇数にする必要
        if (lst[i - 1] + lst[i]) % 2 == 0: #前が偶数
            lst[i] += lst[i - 1] + 1
            ans[i] = 1
        else: #前が奇数
            lst[i] += lst[i - 1]
            lst[i + 1] -= 1
            ans[i] = 0

    if S[i] == 'W': #偶数にする必要
        if (lst[i - 1] + lst[i]) % 2 == 0: #前が偶数
            lst[i] += lst[i - 1]
            ans[i] = 0
            lst[i + 1] -= 1
        else: #前が奇数
            lst[i] += lst[i - 1] + 1
            ans[i] = 1

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

count *= math.factorial(N)
print (count % MOD)

Submission status: Wrong Answer

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

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

lst = [0] * (2 * N + 1)
ans = [-1] * (2 * N + 1) #左で選んだ時1, 右で選んだ時0
lst[0] = 1
ans[0] = 1
for i in range(1, 2 * N):
    if S[i] == 'B': #奇数にする必要
        if (lst[i - 1] + lst[i]) % 2 == 0: #前が偶数
            lst[i] += lst[i - 1] + 1
            ans[i] = 1
        else: #前が奇数
            lst[i] += lst[i - 1]
            lst[i + 1] -= 1
            ans[i] = 0

    if S[i] == 'W': #偶数にする必要
        if (lst[i - 1] + lst[i]) % 2 == 0: #前が偶数
            lst[i] += lst[i - 1]
            ans[i] = 0
            lst[i + 1] -= 1
        else: #前が奇数
            lst[i] += lst[i - 1] + 1
            ans[i] = 1

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

count *= math.factorial(N)
print (count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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
  lcnt = 0
  rcnt = 0
  for i in range(2*N):
    if L[i] == "R":
      ans *= lcnt - rcnt
      rcnt += 1
    else:
      lcnt += 1
      
  print (ans * fc(N))

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n=int(input())
s=list(input())
#前処理
for i in range(0,2*n,2):
    if s[i]=='W':
        s[i]='B'
    else:
        s[i]='W'

#Lの数を数えてRとマッチング
countL=0
ans=1
for i in range(2*n):
    if s[i]=='W':
        countL+=1
    else:
        ans*=countL
        if countL==0:
            print(0)
            exit()
        ans%=mod
        countL-=1
# 答えはansの階乗
for i in range(1,n+1):
    ans*=i
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n=int(input())
s=list(input())
#前処理
for i in range(0,2*n,2):
    if s[i]=='W':
        s[i]='B'
    else:
        s[i]='W'

#Lの数を数えてRとマッチング
countL=0
ans=1
for i in range(2*n):
    if s[i]=='W':
        countL+=1
    else:
        ans*=countL
        if countL==0:
            print(0)
            exit()
            ans%=mod
        countL-=1
# 答えはansの階乗
for i in range(1,n+1):
    ans*=i
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6 + 1000)

words = lambda t : list(map(t, input().split()))
n = int(input())
s = input()

a = [0] * len(s)
for i in range(len(s)):
    if s[i] == "B":
        a[i] = 1
    else:
        a[i] = 0

l = 0
r = len(a)-1

def recur(l,r,cur_n,k):
    #print(l,r,cur_n,k)
    if l >= r:
        return 0
    if l+1 == r:
        if a[l] == a[r] and a[l] == k:
            return 1
        else:
            return 0
    ret = 0
    if a[l] == a[l+1] and a[l] == k:
        ret += recur(l+2,r, cur_n-1,k)
        ret %= (10**9 + 7)
    if a[r] == a[r-1] and a[r] == k:
        ret += recur(l,r-2,cur_n-1,k)
        ret %= (10**9 + 7)
    if a[l] == a[r] and a[l]== k:
        ret += recur(l+1,r-1,cur_n-1,(k+1)%2)
        ret %= (10**9 + 7)
    #print(ret)
    return cur_n * (cur_n - 1) * ret % (10**9 + 7)

print(recur(l,r,n,1))

Submission status: Wrong Answer

Solution code:


n=int(input())
s=list(input())
#前処理
for i in range(0,2*n,2):
    if s[i]=='W':
        s[i]='B'
    else:
        s[i]='W'

#Lの数を数えてRとマッチング
countL=0
ans=1
for i in range(2*n):
    if s[i]=='W':
        countL+=1
    else:
        ans*=countL
        if countL==0:
            print(0)
            exit()
        countL-=1
# 答えはansの階乗
ans=ans%(10**9+7)
for i in range(1,n+1):
    ans*=i
    ans%(10**9+7)
print(ans)

Submission status: Wrong Answer

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

from math import factorial

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

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

print(res * factorial(n) % mod)

Submission status: Wrong Answer

Solution code:
import math

n=int(input())
s=list(input())
#前処理
for i in range(0,2*n,2):
    if s[i]=='W':
        s[i]='B'
    else:
        s[i]='W'

#Lの数を数えてRとマッチング
countL=0
ans=1
for i in range(2*n):
    if s[i]=='W':
        countL+=1
    else:
        ans*=countL
        if countL==0:
            print(0)
            exit()
        countL-=1
# 答えはansの階乗

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

Submission status: Wrong Answer

Solution code:
import math

n=int(input())
s=list(input())
#前処理
for i in range(0,2*n,2):
    if s[i]=='W':
        s[i]='B'
    else:
        s[i]='W'

#Lの数を数えてRとマッチング
countL=0
ans=1
for i in range(2*n):
    if s[i]=='W':
        countL+=1
    else:
        ans*=countL
        if countL==0:
            print(0)
            exit()
        countL-=1
# 答えはansの階乗

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

Submission status: Wrong Answer

Solution code:
import math

n=int(input())
s=list(input())
#前処理
for i in range(0,2*n,2):
    if s[i]=='W':
        s[i]='B'
    else:
        s[i]='W'

#Lの数を数えてRとマッチング
countL=0
ans=1
for i in range(2*n):
    if s[i]=='W':
        countL+=1
    else:
        ans*=countL
        if countL==0:
            print(0)
            exit()
        countL-=1
# 答えはansの階乗

x=math.factorial(n)
print(ans*x)

Submission status: Wrong Answer

Solution code:
import math

n=int(input())
s=list(input())
#前処理
for i in range(0,2*n,2):
    if s[i]=='W':
        s[i]='B'
    else:
        s[i]='W'

#Lの数を数えてRとマッチング
countL=0
ans=1
for i in range(2*n):
    if s[i]=='W':
        countL+=1
    else:
        ans*=countL
        countL-=1
# 答えはansの階乗

x=math.factorial(n)
print(ans*x)

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
s=input()
ns=[]
for i in range(len(s)):
  if i%2==0:
    if s[i]=="B":
      ns.append("L")
    else:
      ns.append("R")
  else:
    if s[i]=="W":
      ns.append("L")
    else:
      ns.append("R")

lcount=0
answer=1
fault=False
for i in range(len(ns)):
  if ns[i]=="L":
    lcount=lcount+1
  else:
    if lcount==0:
      fault=True
      break
    else:
      answer=answer*lcount
      lcount=lcount-1

if lcount!=0:
  fault=True

if fault:
  print(0)
else:
  print(answer%(10**9+7)*(math.factorial(n)))

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
s=input()
ns=[]
for i in range(len(s)):
  if i%2==0:
    if s[i]=="B":
      ns.append("L")
    else:
      ns.append("R")
  else:
    if s[i]=="W":
      ns.append("L")
    else:
      ns.append("R")

lcount=0
answer=1
fault=False
for i in range(len(ns)):
  if ns[i]=="L":
    lcount=lcount+1
  else:
    answer=answer*lcount
    lcount=lcount-1
    if lcount<0:
      fault=True

if lcount!=0:
  fault=True

if fault:
  print(0)
else:
  print(answer%(10**9+7)*(math.factorial(n)))

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce
from operator import mul
N = int(input())
S = input()
mod = 10**9+7
if(not (S.count('B')==N and S[0]==S[-1]=='B')):
    print(0)
else:
    d = []
    cumu = [[0]*2 for _ in range(N*2)]
    cnt = 1
    for ind,s in enumerate(S):
        if(ind==0):
            d.append(0)
            cumu[0][0]+=1
        else:
            if(S[ind-1]==s):
                d.append(1-d[-1])
            else:
                d.append(d[-1])
            cumu[ind][0] = cumu[ind-1][0]
            cumu[ind][1] = cumu[ind-1][1]
            cumu[ind][d[-1]]+=1
            if(d[-1]==1):
                cnt *= (cumu[ind-1][0]-cumu[ind-1][1])
                cnt %= mod
    print((math.factorial(N)*cnt)%mod)

Submission status: Wrong Answer

Solution code:
from math import factorial
n=int(input())
s = [0 if x=='W' else 1 for x in list(input())]
s_diff = [s[i] != s[i+1]for i in range(len(s)-1)]
x = (sum(s_diff) +2) //2 # 黒島の数
y = sum(s_diff) # 切れ目の数

if s[0]==0 or s[-1]==0 or y/2==n:
    print(0)
else:
    print(factorial(x)*y)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
mod = 10**9+7
dp = []
imos = [0 for _ in range(n*2+1)]
now = 0
for i in range(n*2):
    c = 1 if s[i]=='B' else 0
    if c==now%2:
        imos[i+1]-=1
        dp.append(-1)
    else:
        imos[i]+=1
        dp.append(1)
    now+=imos[i]
if now + imos[2*n]!=0:
    print(0)
    exit()
ans = 1
cnt = 0
for i in dp:
    if i==1:
        cnt+=1
    else:
        ans*=cnt
        ans%=mod
        cnt-=1
for i in range(1,n+1):
    ans*=i
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
mod = 10**9+7
dp = []
imos = [0 for _ in range(n*2+1)]
now = 0
for i in range(n*2):
    c = 1 if s[i]=='B' else 0
    if c==now%2:
        imos[i+1]-=1
        dp.append(-1)
    else:
        imos[i]+=1
        dp.append(1)
    now+=imos[i]
if now + imos[i+1]!=0:
    print(0)
    exit()
ans = 1
cnt = 0
for i in dp:
    if i==1:
        cnt+=1
    else:
        ans*=cnt
        ans%=mod
        cnt-=1
for i in range(1,n+1):
    ans*=i
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
mod = 10**9+7
dp = []
imos = [0 for _ in range(n*2+1)]
now = 0
for i in range(n*2):
    c = 1 if s[i]=='B' else 0
    if c==now%2:
        imos[i+1]-=1
        dp.append(-1)
    else:
        imos[i]+=1
        dp.append(1)
    now+=imos[i]
ans = 1
cnt = 0
for i in dp:
    if i==1:
        cnt+=1
    else:
        ans*=cnt
        ans%=mod
        cnt-=1
for i in range(1,n+1):
    ans*=i
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(input())
cnt=0
if l[0]=="W" or l[-1]=="W":
    print(0)
    exit()
#半分に割って、乗り越えるやつと半分で処理すべき部分に分ける xxxx
#自分が左側になるか右側になるか
#目的：隣の色との関係性により、右左のどちらなのかを決める
left=1
right=0
lorr=True
ans=1
for i in range(1,2*n):
    if l[i-1]!=l[i]:
        if lorr:
            left+=1
        else:
            ans*=left
            left-=1
    else:
        lorr=not lorr
        if lorr:
            left+=1
        else:
            ans*=left
            left-=1
mod=10**9+7
ans%=mod
for i in range(1,n+1):
    ans*=i
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(input())
cnt=0
if l[0]=="W" or l[-1]=="W":
    print(0)
    exit()
#半分に割って、乗り越えるやつと半分で処理すべき部分に分ける xxxx
#自分が左側になるか右側になるか
#目的：隣の色との関係性により、右左のどちらなのかを決める
left=1
right=0
lorr=True
ans=1
for i in range(1,2*n):
    if l[i-1]!=l[i]:
        if lorr:
            left+=1
        else:
            ans*=left
            left-=1
    else:
        lorr=not lorr
        if lorr:
            left+=1
        else:
            ans*=left
            left-=1
mod=10**9+7
ans%=mod
tmp=1
for i in range(1,n+1):
    tmp*=i
    tmp%=mod
print(ans*tmp%mod)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(input())
cnt=0
if l[0]=="W" or l[-1]=="W":
    print(0)
    exit()
#半分に割って、乗り越えるやつと半分で処理すべき部分に分ける xxxx
#自分が左側になるか右側になるか
#目的：隣の色との関係性により、右左のどちらなのかを決める
left=1
right=0
lorr=True
ans=1
for i in range(1,2*n):
    if l[i-1]!=l[i]:
        if lorr:
            left+=1
        else:
            ans*=left
            left-=1
    else:
        lorr=not lorr
        if lorr:
            left+=1
        else:
            ans*=left
            left-=1
mod=10**9+7
tmp=1
for i in range(1,n+1):
    tmp*=i
    tmp%=mod
print(ans*tmp)

Submission status: Wrong Answer

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

LR="L"

for i in range(1,len(s)):
  if s[i-1]==s[i]:LR+="R" if LR[i-1]=="L" else "L"
  else:LR+="R" if LR[i-1]=="R" else "L"

if s[0]=="W" or s[-1]=="W" or (s.count("W")!=n and n>1):
  print(0)
  exit()

count=1
for i in range(1,len(s)):
  if LR[i]=="R":
    count=count*(LR[:i].count("L")-LR[:i].count("R"))
    #print(count,LR[:i].count("L"),LR[:i].count("R"))

x=1
for i in range(1,n+1):
  x=x*i
mod=10**9+7
count=count%mod
x=x%mod
#print(LR,count,x)
print((count*x)%mod)

Submission status: Wrong Answer

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

LR="L"

for i in range(1,len(s)):
  if s[i-1]==s[i]:LR+="R" if LR[i-1]=="L" else "L"
  else:LR+="R" if LR[i-1]=="R" else "L"

if s[0]=="W" or s[-1]=="W" or (s.count("W")!=n and n>1):
  print(0)
  exit()

count=1
for i in range(1,len(s)):
  if LR[i]=="R":
    count=count*(LR[:i].count("L")-LR[:i].count("R"))
    #print(count,LR[:i].count("L"),LR[:i].count("R"))

x=1
for i in range(1,n+1):
  x=x*i
mod=10**9+7
#print(LR,count,x)
print((count*x)/mod)

Submission status: Wrong Answer

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

LR="L"

for i in range(1,len(s)):
  if s[i-1]==s[i]:LR+="R" if LR[i-1]=="L" else "L"
  else:LR+="R" if LR[i-1]=="R" else "L"

if s[0]=="W" or s[-1]=="W" or (s.count("W")!=n and n>1):
  print(0)
  exit()

count=1
for i in range(1,len(s)):
  if LR[i]=="R":
    count=count*(LR[:i].count("L")-LR[:i].count("R"))
    #print(count,LR[:i].count("L"),LR[:i].count("R"))

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

#print(LR,count,x)
print((count*x)//(10**9+7))