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

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
using ll = long long;
using ldb = long double;
using pa = pair < ll, ll >;
using vec = vector < ll >;
 
template < typename T > using index_set = tree < T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update >;
template < typename T > using MinPriorityQueue = priority_queue < T, vector < T >, greater < T > >;

#define pb push_back
#define po pop_back
#define mp make_pair
#define mt make_tuple
#define F  first
#define S  second
#define f(i,x,n) for(ll i=x;i<n;i++) 
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))
#define all(c) c.begin(),c.end()
#define str string
#define edl "\n"
#define add insert
#define cot continue
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL)


const int MOD = 1e9 + 7, INF = INT_MAX, N = 1e5 + 10;
const double PI = acos(-1);
const ll LINF = LLONG_MAX;

int main() 
{

 
    fast();
      

    ll n,k;
    cin>>n>>k;
    vec q(n);
    f(i,0,n)
    {
      cin>>q[i];
    }
    ll array[n]={0};
    f(i,0,n)
    {
      f(j,i+1,n)
      {
        if(q[i]>q[j])
        {
          array[i]++;
        }
      }
    }
    ll array1[n]={0};
    f(i,0,n)
    {
      f(j,0,n)
      {
        if(i!=j&&q[i]>q[j])
        {
          array1[i]++;
        }
      }
    }
    

    // f(i,0,n)
    // {
    //   cout<<array[i]<<edl;
    // }
    ll s=0;
    f(i,0,n)
    {
      s=(s%MOD+array[i]%MOD*k%MOD)%MOD;
      s%=MOD;
      
      ll check;
      if(k-2==0)
      {
        check=1;
        s=s%MOD+array[i]%MOD;
        s%=MOD;
      }
      else
      {
        check=k-2;
      
        ll last=(array1[i]%MOD+(check%MOD)*array1[i]%MOD)%MOD;
        ll ff=((k-1)%MOD/2);
        // cout<<s<<" "<<array[i]<<" ";
        s=s%MOD+((ff%MOD)*(array1[i]%MOD+last%MOD)%MOD)%MOD;
      }
      s%=MOD;
      // cout<<last<<' '<<s<<" "<<array1[i]<<edl;


    }
    cout<<(s+MOD)%MOD<<edl;
  

    cerr << "Time taken: " << int((clock() * 1000.) / CLOCKS_PER_SEC) << "ms\n";
    return 0;
}