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: Runtime Error

Solution code:
def fact(x):
    if x == 0:
        return 1
    else:
        return x * fact(x - 1) % mod


mod = 10 ** 9 + 7

n = int(input())
s = input()
t = list(map(lambda x: 1 if x == 'B' else 0, s))
t = tuple((tt - df) % 2 for tt, df in zip(t, reversed(range(n * 2))))
# print(t)

zeros = 0
ans = 1
ones = 0
for tt in t:
    if tt == 0:
        zeros += 1
    else:
        if zeros == 0:
            print(0)
            exit()
        ans *= zeros
        ans %= mod
        zeros -= 1
        ones += 1

if zeros > 0:
    ans = 0

ans *= fact(ones)
ans %= mod
print(ans)

Submission status: Runtime Error

Solution code:
def fact(x):
    if x == 0:
        return 1
    else:
        return x * fact(x - 1) % mod


mod = 10 ** 9 + 7

n = int(input())
s = input()
t = list(map(lambda x: 1 if x == 'B' else 0, s))
t = tuple((tt - df) % 2 for tt, df in zip(t, reversed(range(n * 2))))
# print(t)

zeros = 0
ans = 1
ones = 0
for tt in t:
    if tt == 0:
        zeros += 1
    else:
        ans *= zeros
        ans %= mod
        zeros -= 1
        ones += 1
if zeros > 0:
    ans = 0

ans *= fact(ones)
ans %= mod
print(ans)

Submission status: Runtime Error

Solution code:
def factorial(N):
    if N == 1:
        return N
    return N * factorial(N-1)

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

mod = 10 ** 9 + 7

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

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

ans = 1
t_sum = 0
f_sum = 0
for t in T:
    if t:
        t_sum += 1
    else:
        ans *= (t_sum - f_sum)
        f_sum += 1

if t_sum != f_sum:
    ans = 0

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

Submission status: Runtime Error

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

    stack, c = [1], 1
    for i in range(1, n*2):
        if s[i]=="B":
            if len(stack)%2==0:
                stack.append(1)
            else:
                c *= len(stack)
                stack.pop()
        else:
            if len(stack)%2==0:
                c *= len(stack)
                stack.pop()
            else:
                stack.append(1)
    o = 0 if len(stack)!=0 else c*math.factorial(n)
    print(o)

if __name__ == "__main__":
    f_c()

Submission status: Runtime Error

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

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

rl=[L]
for i in range(1,m):
    if s[i-1]==s[i]:
        if rl[i-1]=="L":
            rl.append("L")
        else: 
            rl.append("R")
    else:
        if rl[i-1]=="R":
            rl.append("L")
        else: 
            rl.append("R")
if rl[m-1]=="L":
    print(0)
    exit()
nl=0
for i in range(m):
    if rl[i]=="L":
        nl+=1
if nl!=n:
    print(0)
    exit()
    
ans=1
li=0
ri=0
for i in range(m):
    if rl[i]=="L":
        li+=1
    else:
      if li-ri<0:
          ans=0
      ans*=li-ri
      ans=ans%mod
      ri+=1
    if i>=1 and i<=n:
        ans*=i
        ans=ans%mod
print(ans)

Submission status: Runtime Error

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)
    else:
        dp[i+1] = 0

print(dp[-1])

Submission status: Runtime Error

Solution code:
import sys
from math import factorial

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

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

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

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

ct_L, ans = 0, 1

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

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

Submission status: Runtime Error

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
    for i in range(2 * n):
        if s[i] == "R":
            ans *= l[i] - r[i] - 1
    print(ans * math.factorial(n))
    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: Runtime Error

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

Submission status: Runtime Error

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 rangea(1,n+1):
    ans*=i
    ans%(10**9+7)
print(ans)

Submission status: Runtime Error

Solution code:
from functools import lru_cache
n = int(input())
s = input()

mod = 10**9+7
bikkuri = lambda x : 1 if x==0 else x*bikkuri(x-1)

@lru_cache(maxsize=mod)
def ocero(start, string, r) :
    if start in [2*n-1, 2*n] :
        if string == "W"*2*n :
            return 1
    elif r[start] :
        return ocero(start+1, string, r)
    elif string[start] == "B" :
        if string[start+1] == "B" :
            ans = 0
            for j in range(start+1, 2*n) :
                if r[j] == 0 :
                    rcopy = tuple(1 if i==j else v for i, v in enumerate(r))
                    newstr = "".join("BW".strip(c) if start <= i <= j else c for i, c in enumerate(string))
                    ans += ocero(start+1, newstr, rcopy)
            return ans
        else :
            ans = 0
            for j in range(start+2, 2*n) :
                if r[j] == 0 :
                    rcopy = tuple(1 if i==j else v for i, v in enumerate(r))
                    newstr = "".join("BW".strip(c) if start <= i <= j else c for i, c in enumerate(string))
                    ans += ocero(start+1, newstr, rcopy)
            return ans
    return 0

reserved = tuple(0 for _ in range(2*n))
print(ocero(0, s, reserved)*bikkuri(n))

Submission status: Runtime Error

Solution code:
import math
n = int(input())
s = [0 if i == "W" else 1 for i in list(input())]
cnt = 0
c = 1
for i in range(n * 2):
    if (s
if cnt != 0:
    print(0)
else:
    print((c * math.factorial(n)) % (10 ** 9 + 7))

Submission status: Runtime Error

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 *= (i - (count*2))
    count += 1
  if s[2*i + 1] == "B":
    ans *= (i - (count*2))
    count += 1
if count != N:
  print(0)
else:
  for o in range(1, N+1):
    ans *= o
    ans %= m
  print(ans %= m)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math
import sys
from copy import copy
import numpy as np
from collections import defaultdict, deque, Counter
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)

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

count = 0


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

LR_list = ['' for i in range(2*n)]
LR_list[0] = 'L'

for i in range(1,len(LR_list)):
    if s[i] == s[i-1]:
        if LR_list[i-1] == 'R':
            LR_list[i] = 'L'
        else:
            LR_list[i] = 'R'
    else:
        if LR_list[i-1] == 'R':
            LR_list[i] = 'R'
        else:
            LR_list[i] = 'L'

values = []

if LR_list.count('L') != LR_list.count('R'):
    print(0)
    sys.exit()


li = 0
ri = 0
for i in range(len(LR_list)):
    if i != 0:
        if LR_list[i-1] == 'L':
            li += 1
        elif LR_list[i-1] == 'R':
            ri += 1
    if LR_list[i] == 'R':
        values.append(li-ri)
value = 1
for i in range(len(values)):
    value = value * values[i] % (10**9+7)
for i in range(2,n+1):
    value = value * i %(10**9+7)
print(value)

Submission status: Runtime Error

Solution code:
def main():
    import math
    import sys
    from copy import copy
    import numpy as np
    from collections import defaultdict, deque, Counter
    from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
    from operator import itemgetter
    from bisect import bisect_left,bisect
    from heapq import heappop,heappush
    from fractions import gcd

    max_value = 1000000000
    min_value = -1000000000
    sys.setrecursionlimit(max_value)

    def input():
        return sys.stdin.readline()[:-1]

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

    count = 0


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

    LR_list = ['' for i in range(2*n)]
    LR_list[0] = 'L'

    for i in range(1,len(LR_list)):
        if s[i] == s[i-1]:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'L'
            else:
                LR_list[i] = 'R'
        else:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'R'
            else:
                LR_list[i] = 'L'

    values = []

    if LR_list.count('L') != LR_list.count('R'):
        print(0)
        sys.exit()


    li = 0
    ri = 0
    for i in range(len(LR_list)):
        if i != 0:
            if LR_list[i-1] == 'L':
                li += 1
            elif LR_list[i-1] == 'R':
                ri += 1
        if LR_list[i] == 'R':
            values.append(li-ri)
    value = 1
    for i in range(len(values)):
        value = value * values[i] % (10**9+7)
    for i in range(2,n+1):
        value = value * i %(10**9+7)
    print(value)


if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    import math
    import sys
    from copy import copy
    import numpy as np
    from collections import defaultdict, deque, Counter
    from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
    from operator import itemgetter
    from bisect import bisect_left,bisect
    from heapq import heappop,heappush
    from fractions import gcd

    max_value = 1000000000
    min_value = -1000000000
    sys.setrecursionlimit(max_value)

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

    count = 0


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

    LR_list = ['' for i in range(2*n)]
    LR_list[0] = 'L'

    for i in range(1,len(LR_list)):
        if s[i] == s[i-1]:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'L'
            else:
                LR_list[i] = 'R'
        else:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'R'
            else:
                LR_list[i] = 'L'

    values = []

    if LR_list.count('L') != LR_list.count('R'):
        print(0)
        sys.exit()


    li = 0
    ri = 0
    for i in range(len(LR_list)):
        if i != 0:
            if LR_list[i-1] == 'L':
                li += 1
            elif LR_list[i-1] == 'R':
                ri += 1
        if LR_list[i] == 'R':
            values.append(li-ri)
    value = 1
    for i in range(len(values)):
        value = value * values[i] % (10**9+7)
    for i in range(2,n+1):
        value = value * i %(10**9+7)
    print(value)



if __name__=='__main__':
    main()

Submission status: Runtime Error

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

    count = 0


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

    LR_list = ['' for i in range(2*n)]
    LR_list[0] = 'L'

    for i in range(1,len(LR_list)):
        if s[i] == s[i-1]:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'L'
            else:
                LR_list[i] = 'R'
        else:
            if LR_list[i-1] == 'R':
                LR_list[i] = 'R'
            else:
                LR_list[i] = 'L'

    values = []

    if LR_list.count('L') != LR_list.count('R'):
        print(0)
        sys.exit()


    li = 0
    ri = 0
    for i in range(len(LR_list)):
        if i != 0:
            if LR_list[i-1] == 'L':
                li += 1
            elif LR_list[i-1] == 'R':
                ri += 1
        if LR_list[i] == 'R':
            values.append(li-ri)
    value = 1
    for i in range(len(values)):
        value = value * values[i] % (10**9+7)
    for i in range(2,n+1):
        value = value * i %(10**9+7)
    print(value)



if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit((10 ** 5) * 2)
 
def kai(a):
  if a == 1:
    return 1
  else:
    return a*kai(a-1)
  
N = int(input())
S = input()
count = 0
ans = 1
 
for i, s in enumerate(S):
  if ((s == "W") & (i%2 == 0)) or ((s == "B") & (i%2 == 1)):
    ans *= (i - (count*2))
    count += 1

other = N - count
if other != 0:
  ans *= (kai(other*2)//(2**other))
 
print((ans*kai(N))%(10**9 + 7))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit((10 ** 5) * 2)
 
def kai(a):
  if a == 1:
    return 1
  else:
    return a*kai(a-1)
  
N = int(input())
S = input()
count = 0
ans = 1
 
for i, s in enumerate(S):
  if ((s == "W") & (i%2 == 0)) or ((s == "B") & (i%2 == 1)):
    ans *= (i - (count*2))
    count += 1

other = N - count
if other != 0:
  ans *= (kai(other*2)//(2**other))
 
print(kai(ans)%(10**9 + 7))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit((10 ** 5) * 2)

def kai(a):
  if a == 1:
    return 1
  else:
    return a*kai(a-1)
  
N = int(input())
S = input()
count = 0
ans = 1

for i, s in enumerate(S):
  if ((s == "W") & (i%2 == 0)) or ((s == "B") & (i%2 == 1)):
    ans *= (i - (count*2))
    count += 1

other = N - count
ans *= (kai(other*2)//(2**other))

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

Submission status: Runtime Error

Solution code:
import math
import sys
from copy import copy
import numpy as np
from collections import defaultdict, deque, Counter 
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)

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

count = 0


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

LR_list = ['' for i in range(2*n)]
LR_list[0] = 'L'

for i in range(1,len(LR_list)):
    if s[i] == s[i-1]:
        if LR_list[i-1] == 'R':
            LR_list[i] = 'L'
        else:
            LR_list[i] = 'R'
    else:
        if LR_list[i-1] == 'R':
            LR_list[i] = 'R'
        else:
            LR_list[i] = 'L'

values = []

if LR_list.count('L') != LR_list.count('R'):
    print(0)
    sys.exit()
for i in range(len(LR_list)):
    if LR_list[i] == 'R':
        values.append(li-ri)
        li = LR_list[:i].count('L')
        ri = LR_list[:i].count('R')
print(np.array(values).prod()*math.factorial(n))

Submission status: Runtime Error

Solution code:
MOD = 10**9+7

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

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

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

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

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

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


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

ans *= kai[n]
ans %= MOD

print(ans)

Submission status: Runtime Error

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):
  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: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

MOD = 10**9+7

def main():
    n, k = rli()
    la = rli()
    rev1 = 0
    rev2 = 0
    for i in range(len(la)):
        for j in range(len(la)):
            if la[i] > la[j]:
                if i < j:
                    rev1 += 1
                else:
                    rev2 += 1
    ans = rev1 * k*(k+1)//2 + rev2 * (k-1)*k//2
    ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import copy
import sys

input = sys.stdin.readline
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = copy.deepcopy(a)

w = 10 ** 9 + 7

cnta = 0
cntb = 0
for i in range(len(a)):
    for j in range(len(a) - 1, i, -1):
        if a[j] < a[j - 1]:
            a[j], a[j - 1] = a[j - 1], a[j]

            cnta += 1

for i in range(len(b)):
    for j in range(len(b)):
        if b[i] > b[j]:
            cntb += 1

wa = (k * (k - 1) // 2) * cntb + cnta * k
print(wa % w)

Submission status: Runtime Error

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]=='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: Runtime Error

Solution code:
import numpy as np
import math
import sys
sys.setrecursionlimit(10000000)
MOD = 10**9 + 7

def main():
    N, K = map(int, input().split())   
    A = [int(c) for c in input().split()]
    num = 0
    for i in range(N):
        small = 0
        large = 0
        for j in range(N):
            if i == j:
                continue
            elif i < j:
                # 自分より右側に小さい
                if A[i] > A[j]:
                    small += 1
            else:
                if A[i] < A[j]:
                    large += 1
        num = small * 0.5 * (K - 1)% MOD  * K + large * 0.5 * K % MOD  * (K + 1)
    num %= MOD 
    print(int(num))



if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

def hanten(string):
    ans = ''
    for i in range(len(string)):
        if string[i] == 'B':
            ans += 'W'
        else:
            ans += 'B'
    return ans

def saiki(string, baai):
    if string == 'BB':
        return baai + 1
    elif string[0] == 'B':
        for i in range(1, len(string)):
            if i % 2 == 1 and string[i] == 'B':
                new_string = hanten(string[1:i]) + string[i+1:]
                baai = saiki(new_string, baai)
            elif i % 2 == 0 and string[i] == 'W':
                new_string = hanten(string[1:i]) + string[i+1:]
                baai = saiki(new_string, baai)
    else:
        return baai
    return baai

baai = saiki(s, 0)

ans = 1
for i in range(1, n+1):
    ans *= i
    ans = ans % (10**9+7)

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

Submission status: Runtime Error

Solution code:
from math import factorial
from collections import deque
from copy import deepcopy
n = int(input())
s = list(map(lambda x: 0 if x=="W" else 1, list(input())))
# available: [0,1,2,3] s: [1,0,0,1]
available = [i for i in range(n*2)]
global patterns = 0
def f(snew, available):
    if not len(available):
        if sum(snew) == 0: patterns +=1
        else: return 
    i = available.pop(0)
    for j in range(len(available)):
        snext = []
        for colori in range(n):
            if i <= colori <= j:
                snext.append(snew[colori])
            else:
                snext.append(snew[colori] ^ 1)
        f(snext, available[:j] + available[j+1:])
f(s,available)
print(patterns * factorial(n))

Submission status: Runtime Error

Solution code:
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]:
            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)
    if a[r] == a[r-1] and a[r] == k:
        ret += recur(l,r-2,cur_n-1,k)
    if a[l] == a[r] and a[l]== k:
        ret += recur(l+1,r-1,cur_n-1,(k+1)%2)
    #print(ret)
    return cur_n * (cur_n - 1) * ret % (10**9 + 7)

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

Submission status: Runtime Error

Solution code:
from math import factorial
from collections import deque
from copy import deepcopy
n = int(input())
s = list(map(lambda x: 0 if x=="W" else 1, list(input())))
# available: [0,1,2,3] s: [1,0,0,1]
available = [i for i in range(n*2)]
patterns = 0
def f(snew, available):
    if not len(available):
        if sum(snew) == 0: patterns +=1
        else: return 
    i = available.pop(0)
    for j in range(len(available)):
        snext = []
        for colori in range(n):
            if i <= colori <= j:
                snext.append(snew[colori])
            else:
                snext.append(snew[colori] ^ 1)
        f(snext, available[:j] + available[j+1:])
f(s,available)
print(patterns * factorial(n))

Submission status: Runtime Error

Solution code:
from scipy.special import comb

n = int(input())

s = input()

Q = pow(10,9) + 7

p = -1
q = ""
r = ""
flg = True
for i in range(n):
  if(i%2 == 0):
    if(s[i] == s[2*n-i-1] == "B"):
      continue
    else:
      p = i
      q = s[i]
      r = s[2*n-i-1]
      break

  else:
    if(s[i] == s[2*n-i-1] == "W"):
      continue
    else:
      p = i
      q = s[i]
      r = s[2*n-i-1]
      break

if(p >= 0):
  for i in range(p,n):
    if(s[i] == q and s[2*n-i-1] == r):
      continue
    else:
      flg = False
      break

if(not(flg)):
  print(0)

else:
  bnum = s.count("B")
  wnum = 2*n - bnum
  print( int(comb(bnum,2))%Q * int(comb(wnum,2))%Q * n * 2%Q  )

Submission status: Runtime Error

Solution code:
n = int(input())
s = str(input())
MOD = 10**9 + 7

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

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

ans = 1
count = 0
pre = s[0]
for i in range(1, 2*n):
    if s[i] == pre:
        count += 1
    else:
        ans *= kai[count+1]
        ans %= MOD
        count = 0
    pre = s[i]

ans *= kai[n]
ans %= MOD
print(ans)