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:
import math

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

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

Submission status: Wrong Answer

Solution code:
MOD = 10 ** 9 + 7

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

  D = [ True ] * (2 * N) # L: True, R: False
  d = True
  for i in range(1, N * 2):
    if S[i] == S[i - 1]: d = not d
    D[i] = d

  C = 1
  c = 0
  for d in D:
    if d: c += 1
    else:
      C *= c
      c -= 1
      if c < 0: return 0

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

  return C

print(solve())

Submission status: Wrong Answer

Solution code:
import math

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


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
s = input()
ar = [0]*(2*n) # 各点を選んだ操作の時の相方のマスがある方向。右側なら０、左なら１
ans = 0
c = 1
res = 0
if s[0] == "W" or s[-1] == "W":
    print(ans)
    exit(0) # 端点がWなら無理
for i in range(2*n-1):
    if s[i] == s[i+1]: #反転しているなら、操作の時の向きは同じ
        ar[i+1] = (ar[i]+1)%2
    else:
        ar[i+1] = ar[i]
    if ar[i+1] == 1: # 1の方を基準にしてペアの選び方がいくつあるかを数える。今の１より左に何個０があるかをかけていく
        #print(c,res,i)
        c *= (i+1-res) % 1000000007
        res += 2
# ar[0] = 0 なので初期化はこのままでOK
#print(ar)
#ans = (math.factorial(n) % 1000000007) * ( c % 1000000007) % 1000000007 # 操作は右向きと左向きのものをペアになるように自由に選ぶのでn!*
ans = math.factorial(n)  *  c  % 1000000007 # 操作は右向きと左向きのものをペアになるように自由に選ぶのでn!*

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
ls=[]
for i in range(N):
  if S[i]=='B':
    f=1
  else:
    f=0
  ls.append((f+i)%2)
count_sub=0
ans=1
for i in range(N):
  if ls[i]==1:
    count_sub+=1
  else:
    ans=(ans*count_sub)%1000000007
    count_sub-=1
for i in range(1,(N//2)+1):
  ans=(ans*i)%1000000007
print(ans)

Submission status: Wrong Answer

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

dp = [0]*n

for i in range(n):
    sentou = s.lstrip()
    matsubi = s.rstrip()
    if (i%2 == 0) and (sentou == 'B') and (matsubi == 'B'):
        dp[i+1] = dp[i] * math.factorial(i)
    elif (i%2 == 1) and (sentou == 'W') and (matsubi == 'W'):
        dp[i+1] = dp[i] * math.factorial(i)
        

#BとW入れ替えしたかった
print(dp[-1])

Submission status: Wrong Answer

Solution code:
def p_c():
    N = int(input())
    S = input()
    ans = 2
    for i in range(2 * N):
        if S[i] == "W":
            continue
        l = i - 1
        while l >= 0 and S[l] != "W":
            l -= 1

        r = i + 1
        while r < N and S[r] != "W":
            r += 1

        if l >= 0:
            ans *= i - l + 1
        if r < 2 * N:
            ans *= r - i + 1

    print(ans)


if __name__ == '__main__':
    p_c()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
mod = 1000000007


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


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


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

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

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

main()

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
S = input()
ans = 0
ans_base = 1
numofS = 0  # 線の左端になっている点をSとするとその数はちょうどNでないといけない
line = 0  # 左から順に線を引く。上に何本通っているかをあらわす
for i in range(2*N):
    if S[i] == "B":
        if line % 2 == 0:
            numofS += 1
            line += 1
        else:
            ans_base *= line
            line -= 1
    else:
        if line % 2 == 0:
            ans_base *= line
            line -= 1
        else:
            line += 1
            numofS += 1

    if line < 0:
        ans = -1
if numofS != N:
    ans = -1
    #print(numofS)

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

Submission status: Wrong Answer

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

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

#### 定数 ####
mod = 10**9 + 7

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

#################

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

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

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

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

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

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

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

ans *= fact(N)
ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline

    import collections

    n = int(input())
    ss = input()
    count = 1

    num_linear = 1
    if ss[0] == 'W':
        print(0)
        exit()
    if ss[-1] == 'W':
        print(0)
        exit()
    for s in ss[1:-2]:
        if s == 'W':
            if num_linear % 2 == 0:
                count *= num_linear
                num_linear -= 1
            else:
                num_linear += 1
        else:
            if num_linear % 2 == 1:
                count *= num_linear
                num_linear -= 1
            else:
                num_linear += 1
    ans = 1
    for i in range(1,n+1):
        ans *= i
        ans = ans % (10**9+7)
    ans *= count

    print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

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

#### 定数 ####
mod = 10**9 + 7

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

#################

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

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

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

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

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

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

ans *= fact(N)
ans %= mod

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline

    import collections

    n = int(input())
    ss = input()
    count = 1

    num_linear = 1
    if ss[0] == 'W':
        print(0)
        exit()
    if ss[-1] == 'W':
        print(0)
        exit()
    for s in ss[1:-2]:
        if s == 'W':
            if num_linear % 2 == 0:
                count *= num_linear
                num_linear -= 1
            else:
                num_linear += 1
        else:
            if num_linear % 2 == 1:
                count *= num_linear
                num_linear -= 1
            else:
                num_linear += 1
    ans = 1
    for i in range(1,n+1):
        ans *= i
    ans *= count

    print(int(ans)%(10**9+7))

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
s = input()
ar = [0]*(2*n) # 各点を選んだ操作の時の相方のマスがある方向。右側なら０、左なら１
ans = 0
c = 1
res = 0
if s[0] == "W" or s[-1] == "W":
    print(ans)
    exit(0) # 端点がWなら無理
for i in range(2*n-1):
    if s[i] == s[i+1]: #反転しているなら、操作の時の向きは同じ
        ar[i+1] = (ar[i]+1)%2
    else:
        ar[i+1] = ar[i]
    if ar[i+1] == 1:
        #print(c,res,i)
        c *= (i+1-res)
        res += 2
# ar[0] = 0 なので初期化はこのままでOK
#print(ar)
#ans = (math.factorial(n) % 1000000007) * ( c % 1000000007) % 1000000007 # 操作は右向きと左向きのものをペアになるように自由に選ぶのでn!*
ans = math.factorial(n)  *  c  % 1000000007 # 操作は右向きと左向きのものをペアになるように自由に選ぶのでn!*

print(ans)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
s = input()
ar = [0]*(2*n) # 各点を選んだ操作の時の相方のマスがある方向。右側なら０、左なら１
ans = 0
c = 1
res = 0
if s[0] == "W" or s[-1] == "W":
    print(ans)
    exit(0) # 端点がWなら無理
for i in range(2*n-1):
    if s[i] == s[i+1]: #反転しているなら、操作の時の向きは同じ
        ar[i+1] = (ar[i]+1)%2
    else:
        ar[i+1] = ar[i]
    if ar[i+1] == 1:
        #print(c,res,i)
        c *= (i+1-res)
        res += 2
# ar[0] = 0 なので初期化はこのままでOK
#print(ar)
ans = (math.factorial(n) % 1000000007) * ( c % 1000000007) % 1000000007 # 操作は右向きと左向きのものをペアになるように自由に選ぶのでn!*
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n=int(input())
    s=input()
    MOD=10**9+7
    perm=[1]*(n+1)
    for i in range(1,n+1):
        perm[i]=perm[i-1]*i%MOD
    ans=1
    dp=[0]*(2*n+1)
    for i in range(1,2*n+1):
        if (dp[i-1]+1)%2==(s[i-1]=='B'):
            dp[i]=(dp[i-1]+1)%MOD
        else:
            ans*=dp[i-1]%MOD
            dp[i]=(dp[i-1]-1)%MOD
    #print(dp)
    print(ans*perm[n]%MOD)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
if S.count("B")==N:
    mod=10**9+7
    ans=1
    for i in range(1,N+1):
        ans=(i*ans)%mod
    ans*=N
    ans%=mod
    ans*=(N-1)
    ans%=mod
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
MOD=10**9+7
perm=[1]*(n+1)
for i in range(1,n+1):
    perm[i]=perm[i-1]*i%MOD
ans=1
dp=[0]*(2*n+1)
for i in range(1,2*n+1):
    if (dp[i-1]+1)%2==(s[i-1]=='B'):
        dp[i]=dp[i-1]+1
    else:
        ans*=dp[i-1]%MOD
        dp[i]=dp[i-1]-1
#print(dp)
print(ans*perm[n]%MOD)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
a=[]
for c in s:
    if(c=="B"):
        a.append(-1)
    else:
        a.append(1)

kako=[]

mi=0
kanou=True

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

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

Submission status: Wrong Answer

Solution code:
# c
from math import factorial
M = 10**9+7
n = int(input())
s = input()
if (s[0] != 'B') | (s[-1] != 'B'): print(0); exit()
a = [1]+[0]*(2*n-1)
b = [0]*2*n
for i in range(1, 2*n):
    if s[i] == s[i-1]:
        a[i] = 1^a[i-1]
        b[i] = 1^b[i-1]
    else:
        a[i] = a[i-1]
        b[i] = b[i-1]
for i in range(1, 2*n):
    a[i] += a[i-1]-b[i]
c = 1
for i, j in zip(a, b[1:]):
    if j: c *= i*j; c %= M
for i in range(1, n+1):
    c = c*i%M
if b[-1] == 0:
    print(0)
    exit()
print(c)

Submission status: Wrong Answer

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

dp = [0]*n

for i in range(n):
    sentou = s.lstrip()
    matsubi = s.rstrip()
    if (sentou == 'B') and (matsubi == 'B'):
        dp[i+1] = dp[i] * math.factorial(i)

#BとW入れ替えしたかった
print(dp[-1])

Submission status: Wrong Answer

Solution code:
import math

def nCr(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


n = int(input())
s = input()
if s.count("B") == n:
    print(math.factorial(n) * nCr(n, 2) % (10 ** 9 + 7))
else:
    print(0)

Submission status: Wrong Answer

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

    ans = 1
    for k in range(1,N):
    #    print(k+1,S[k])
        if S[k] == "W" and (k+1)%2 == 0:
            ans *= k+1
        elif S[k] == "W" and (k+1)%2 == 1:
            ans *= k
        elif S[k] == "B" and (k+1)%2 == 1:
            ans *= k+1
        else:
            ans *= k

    for k in range(N,2*N-1):
    #    print(2*N-k,S[k])
        if S[k] == "W" and (2*N-k)%2 == 0:
            ans *= 2*N-k
        elif S[k] == "W" and (2*N-k)%2 == 1:
            ans *= 2*N-k - 1
        elif S[k] == "B" and (2*N-k)%2 == 1:
            ans *= 2*N-k
        else:
            ans *= 2*N-k - 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

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

c = 0
pre_s = S[0]
for s in S[1:]:
    if pre_s != s:
        c += 1
        pre_s = s
    else:
        pass

if c != N:
    print(0)
else:
    print(math.factorial(N)*12)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
mod = 10 ** 9 + 7
bseq = 0
bdot = 0
l = -2

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)  

for i in range(2 * N):
  if S[i] == "W":
    if l == -2:
      l = i
  else:
    if l != -2:
      if l == i - 1:
        bdot += 1
      else:
        bseq += 1
      l = -2
if l != -2:
  if l == 2 * N - 1:
    bdot += 1
  else:
    bseq += 1
if bdot + bseq < N:
  if (N - bdot - bseq) % 2 == 0:
    print(0)
    exit(0)
print(f.factorial(N) * N * (N - 1) % mod)

Submission status: Wrong Answer

Solution code:
import math

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

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

print(cnt*math.factorial(N))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
MOD=10**9+7
c=[0]*(2*n)
perm=[1]*(n+1)
for i in range(1,n+1):
    perm[i]=perm[i-1]*i%MOD
ans=1
for i in range(2*n):
    if s[i]=='B':
        c[i]=1
dp=[0]*(2*n+1)
for i in range(1,2*n+1):
    if i%2==c[i-1]:
        dp[i]=dp[i-1]+1
    else:
        ans*=dp[i-1]
        dp[i]=dp[i-1]-1
#print(dp)
print(ans*perm[n]%MOD)

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
N = int(input())
S = input()
tmp = [i for i in range(1, N+1)]
tmp += [i for i in range(N, 0, -1)]
ans = 1
for ii, si in enumerate(S):
    i = tmp[ii]
    if si == 'W':
        ans *= (i - (i % 2)) % mod
    else:
        ans *= (i - ((i+1) % 2)) % mod

print(ans)

Submission status: Wrong Answer

Solution code:
from math import factorial
M = 10**9+7
n = int(input())
s = input()
if (s[0] != 'B') | (s[-1] != 'B'): print(0); exit()
a = [1]+[0]*(2*n-1)
b = [0]*2*n
for i in range(1, 2*n):
    if s[i] == s[i-1]:
        a[i] = 1^a[i-1]
        b[i] = 1^b[i-1]
    else:
        a[i] = a[i-1]
        b[i] = b[i-1]
for i in range(1, 2*n):
    a[i] += a[i-1]-b[i]
c = 1
for i, j in zip(a, b[1:]):
    if j: c *= i*j; c %= M
for i in range(1, n+1):
    c = c*i%M
print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
A = [i+1 for i in range(N)]
A = A + A[::-1]

bc = 0
wc = 0

ans = 1
for i,s in enumerate(S) :
    if i < N :
        if s == "B" :
            bc += 1
            if i % 2 == 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i > 0 :
                ans *= A[i-1]
                ans %= 10**9+7
            else :
                ans = 0
        else :
            wc += 1
            if i % 2 != 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i > 0 :
                ans *= A[i-1]
                ans %= 10**9+7
            else :
                ans = 0

    else :
        if s == "B" :
            bc += 1
            if i % 2 != 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i < 2*N-1 :
                ans *= A[i+1]
                ans %= 10**9+7
            else :
                ans = 0
        else :
            wc += 1
            if i % 2 == 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i < 2*N-1 :
                ans *= A[i+1]
                ans %= 10**9+7
            else :
                ans = 0

ans %= 10**9+7

if bc > wc :
    ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
import  math
n = int(input())
s = input()
inv_cnt = []
cnt1 = 0
cnt2 = 0
for i in s:
    if i == "B":
        inv_cnt.append(1)
        cnt1 += 1
    else:
        inv_cnt.append(2)
        cnt2 += 1

if cnt1 <= 1 or cnt2 <= 1:
    ans = 0
else:
    ans = math.factorial(cnt1 - 1)
    ans %= 10 ** 9 + 7
# print(ans)
    ans += math.factorial(cnt2 - 1)
    ans %= 10 ** 9 + 7
# print(ans)
    ans *= math.factorial(n)
    ans %= 10 ** 9 + 7

# ans = 1
# key = 1
# # print(inv_cnt)
# yokuwakaranairisuto = [key]
# for i in range(1, 2 * n):
#     if inv_cnt[i] != inv_cnt[i - 1]:
#         # print(i, inv_cnt[i])
#         key += 1
#     yokuwakaranairisuto.append(key)
#
# yokuwakaranairisuto[-1] = 1
# key = 1
# for i in range(2 * n - 2, 0, -1):
#     if inv_cnt[i] != inv_cnt[i + 1]:
#         # print(i, inv_cnt[i])
#         key += 1
#     yokuwakaranairisuto[i] = min(yokuwakaranairisuto[i], key)
# # print(yokuwakaranairisuto)
#
# ans = 1
# for i in range(1, 2 * n):
#     if yokuwakaranairisuto[i] != yokuwakaranairisuto[i - 1]:
#         ans *= yokuwakaranairisuto[i]
#         ans %= 10 ** 9 + 7
# if ans == 1:
#     ans = 0
# ans *= math.factorial(n)
ans %= 10 ** 9 + 7
print(ans)

Submission status: Wrong Answer

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

ans = 1
for k in range(1,N):
#    print(k+1,S[k])
    if S[k] == "W" and (k+1)%2 == 0:
        ans *= k+1
    elif S[k] == "W" and (k+1)%2 == 1:
        ans *= k
    elif S[k] == "B" and (k+1)%2 == 1:
        ans *= k+1
    else:
        ans *= k

for k in range(N,2*N-1):
#    print(2*N-k,S[k])
    if S[k] == "W" and (2*N-k)%2 == 0:
        ans *= 2*N-k
    elif S[k] == "W" and (2*N-k)%2 == 1:
        ans *= 2*N-k - 1
    elif S[k] == "B" and (2*N-k)%2 == 1:
        ans *= 2*N-k
    else:
        ans *= 2*N-k - 1

print(ans)

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
N = int(input())
S = input()
tmp = [i for i in range(1, N+1)]
tmp += [i for i in range(N, 0, -1)]
ans = 1
for si, i in zip(S, tmp):
    if si == 'W':
        ans *= (i - (i % 2)) % mod
    else:
        ans *= (i - ((i+1) % 2)) % mod

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
A = [i+1 for i in range(N)]
A = A + A[::-1]

bc = 0
wc = 0

ans = 1
for i,s in enumerate(S) :
    if i < N :
        if s == "B" :
            bc += 1
            if i % 2 == 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i > 0 :
                ans *= A[i-1]
                ans %= 10**9+7
            else :
                ans = 0
        else :
            wc += 1
            if i % 2 != 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i > 0 :
                ans *= A[i-1]
                ans %= 10**9+7
            else :
                ans = 0

    else :
        if s == "B" :
            bc += 1
            if i % 2 != 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i < 2*N-1 :
                ans *= A[i+1]
                ans %= 10**9+7
            else :
                ans = 0
        else :
            wc += 1
            if i % 2 == 0 :
                ans *= A[i]
                ans %= 10**9+7
            elif i < 2*N-1 :
                ans *= A[i+1]
                ans %= 10**9+7
            else :
                ans = 0

ans %= 10**9+7

if bc != wc :
    ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline

N, = map(int, input().split())
L = list(map(lambda x: 1 if x == "B" else 2, input().strip()))
c = [0] * len(L)
c[0] = 1
c[-1] = 1

if L[0] == 2 or L[-1] == 2:
    print(0)
    exit()

for i in range(len(L) - 2):
    if L[i] == L[i + 1]:
        c[i + 1] += c[i]
    else:
        c[i + 1] += c[i] + 1

ans = 1
for x in c:
    ans *= x % (10 ** 9 + 7)

print(ans % (10 ** 9 + 7) // N)

Submission status: Wrong Answer

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

MOD = int(10**9 + 7)

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

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

    return x



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

    print(f(S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline

N, = map(int, input().split())
L = list(map(lambda x: 1 if x == "B" else 2, input().strip()))
c = [0] * len(L)
c[0] = 1
c[-1] = 1

if L[0] == 2 or L[-1] == 2:
    print(0)
    exit()

for i in range(len(L) - 2):
    if L[i] == L[i + 1]:
        c[i + 1] += c[i]
    else:
        c[i + 1] += c[i] + 1

print(c)

ans = 1
for x in c:
    ans *= x % (10 ** 9 + 7)

print(ans % (10 ** 9 + 7) // N)

Submission status: Wrong Answer

Solution code:
from math import factorial
M = 10**9+7
n = int(input())
s = input()
if (s[0] != 'B') | (s[-1] != 'B'): print(0); exit()
a = [1]+[0]*(2*n-1)
b = [0]*2*n
for i in range(1, 2*n):
    if s[i] == s[i-1]:
        a[i] = 1^a[i-1]
        b[i] = 1^b[i-1]
    else:
        a[i] = a[i-1]
        b[i] = b[i-1]
for i in range(1, 2*n):
    a[i] += a[i-1]-b[i]
c = 1
for i, j in zip(a, b[1:]):
    if j: c *= i*j%M; c %= M
print(c*factorial(n)%M)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
 
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline  #危険！基本オフにしろ！
 
def main():
    l=0
    n,a=open(0).read().split()
    n=int(n)
    
    l=a.find("B")
    r=a.rfind("B")
    if l==-1 or r==-1 or l==r:
        print(0)
        exit()
    a=a[l:r+1]
    wakeme=len([k for k,v in groupby(a)])-1
    if wakeme>n:
        print(0)
        exit()
    
    c=1
    mod=10**9+7
    print((factorial(n)%mod)*(n-1)*( wakeme %mod)%mod)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
L = list(S)
for i in range(0, 2*N, 2):
  L[i] = "B" if L[i] == "W" else "W"
S = "".join(L)
#print(S)
r = 1
i = 0
nw = 0
nb = 0
while i < 2*N:
  j = S.find("B", i)
  if j < 0:
    j = 2*N
  k = S.find("W", j)
  if k < 0:
    k = 2*N
  #print(i, j, k)
  nw += j-i
  nb += k-j
  for i in range(min(nb, nw)):
    r = r*max(nw, nb)%1000000007
    nw -= 1
    nb -= 1
  i = k
if not(nw == 0 and nb == 0):
  print(0)
  exit()
for i in range(1, N+1):
  r = r*i%1000000007
print(r)

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
N = int(input())
S = input()
tmp = [i for i in range(1, N+1)]
tmp += [i for i in range(N, 0, -1)]
ans = 1
for si, i in zip(S, tmp):
    if si == 'W':
        ans *= (i - (i % 2))
    else:
        ans *= (i - ((i+1) % 2))
print(ans % mod)

Submission status: Wrong Answer

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

ans = 1
for k in range(1,N):
#    print(k+1,S[k])
    if S[k] == "W" and (k+1)%2 == 0:
        ans *= k+1
    elif S[k] == "W" and (k+1)%2 == 1:
        ans *= k
    elif S[k] == "B" and (k+1)%2 == 1:
        ans *= k+1
    else:
        ans *= k

for k in range(N,2*N-1):
    print(2*N-k,S[k])
    if S[k] == "W" and (2*N-k)%2 == 0:
        ans *= 2*N-k
    elif S[k] == "W" and (2*N-k)%2 == 1:
        ans *= 2*N-k - 1
    elif S[k] == "B" and (2*N-k)%2 == 1:
        ans *= 2*N-k
    else:
        ans *= 2*N-k - 1

print(ans)