| Task: | Ruudukko | 
| Sender: | Olli | 
| Submission time: | 2018-10-02 11:15:31 +0300 | 
| Language: | C++ | 
| Status: | READY | 
| Result: | 0 | 
| group | verdict | score | 
|---|---|---|
| #1 | WRONG ANSWER | 0 | 
| #2 | WRONG ANSWER | 0 | 
| #3 | WRONG ANSWER | 0 | 
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | WRONG ANSWER | 0.01 s | 1 | details | 
| #2 | WRONG ANSWER | 0.02 s | 1 | details | 
| #3 | WRONG ANSWER | 0.02 s | 1 | details | 
| #4 | WRONG ANSWER | 0.02 s | 1 | details | 
| #5 | WRONG ANSWER | 0.01 s | 1 | details | 
| #6 | WRONG ANSWER | 0.03 s | 1 | details | 
| #7 | WRONG ANSWER | 0.02 s | 1 | details | 
| #8 | WRONG ANSWER | 0.03 s | 1 | details | 
| #9 | WRONG ANSWER | 0.01 s | 1 | details | 
| #10 | WRONG ANSWER | 0.02 s | 1 | details | 
| #11 | WRONG ANSWER | 0.02 s | 2 | details | 
| #12 | WRONG ANSWER | 0.02 s | 2 | details | 
| #13 | WRONG ANSWER | 0.02 s | 2 | details | 
| #14 | WRONG ANSWER | 0.03 s | 2 | details | 
| #15 | WRONG ANSWER | 0.03 s | 2 | details | 
| #16 | WRONG ANSWER | 0.04 s | 2 | details | 
| #17 | WRONG ANSWER | 0.04 s | 2 | details | 
| #18 | WRONG ANSWER | 0.05 s | 2 | details | 
| #19 | WRONG ANSWER | 0.05 s | 2 | details | 
| #20 | WRONG ANSWER | 0.06 s | 2 | details | 
| #21 | WRONG ANSWER | 0.05 s | 3 | details | 
| #22 | WRONG ANSWER | 0.05 s | 3 | details | 
| #23 | WRONG ANSWER | 0.05 s | 3 | details | 
| #24 | WRONG ANSWER | 0.06 s | 3 | details | 
| #25 | WRONG ANSWER | 0.05 s | 3 | details | 
| #26 | WRONG ANSWER | 0.05 s | 3 | details | 
| #27 | WRONG ANSWER | 0.05 s | 3 | details | 
| #28 | WRONG ANSWER | 0.05 s | 3 | details | 
| #29 | WRONG ANSWER | 0.05 s | 3 | details | 
| #30 | WRONG ANSWER | 0.05 s | 3 | details | 
Compiler report
input/code.cpp: In function 'int main()':
input/code.cpp:193:6: warning: unused variable 'f4' [-Wunused-variable]
  int f4 = n - amB - aA;
      ^~
input/code.cpp:194:6: warning: unused variable 'f1' [-Wunused-variable]
  int f1 = amB;
      ^~Code
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 555;
const int M = 1e9 + 7;
char t[N][N];
ll n;
int aA;
int amB;
void swaR(int a, int b) {
	for(int x = 1; x <= n; ++x) {
		char c = t[a][x];
		char d = t[b][x];
		t[a][x] = d;
		t[b][x] = c;
	}
}
void swaC(int a, int b) {
	for(int y = 1; y <= n; ++y) {
		char c = t[y][a];
		char d = t[y][b];
		t[y][a] = d;
		t[y][b] = c;
	}
}
void print() {
	cout << "\n\n";
	for(int y = 1; y <= n; ++y) {
		for(int x = 1; x <= n; ++x) {
			cout << t[y][x];
		}
		cout << "\n";
	}
	cout << "\n";
}
void sor() {
	vector<int> broken;
	for(int y = n; y >= n - amB + 1; --y) {
		bool conB = false;
		for(int x = 1; x <= n; ++x) {
			if(t[y][x] == 'B') {
				conB = true;
				break;
			}
		}
		if(!conB) broken.push_back(y);
	}
	for(int y = n - amB; y >= 1; --y) {
		bool conB = false;
		for(int x = 1; x <= n; ++x) {
			if(t[y][x] == 'B') {
				conB = true;
				break;
			}
		}
		if(!conB) {
			continue;
		}
		int k = broken[broken.size() - 1];
		swaR(k, y);
		broken.pop_back();
	}
	for(int y = n; y >= n - amB + 1; --y) {
		int col = -1;
		for(int x = 1; x <= n; ++x) {
			if(t[y][x] == 'B') {
				col = x;
			}
		}
		swaC(n - y + 1, col);
	}
	aA = 0;
	for(int x = amB + 1; x <= n; ++x) {
		for(int y = n - amB; y >= 1; --y) {
			if(t[y][x] == 'A') {
				++aA;
			}
		}
	}
	for(int y = n - amB; y >= n - amB - aA + 1; --y) {
		bool conA = false;
		for(int x = amB + 1; x <= n; ++x) {
			if(t[y][x] == 'A') {
				conA = true;
				break;
			}
		}
		if(!conA) {
			broken.push_back(y);
		}
	}
	for(int y = n - amB - aA; y >= 1; --y) {
		bool conA = false;
		for(int x = amB + 1; x <= n; ++x) {
			if(t[y][x] == 'A') {
				conA = true;
				break;
			}
		}
		if(conA) {
			int k = broken[broken.size() - 1];
			swaR(k, y);
			broken.pop_back();
		}
	}
	for(int y = n - amB; y >= n - amB - aA + 1; --y) {
		int col = -1;
		for(int x = amB + 1; x <= n; ++x) {
			if(t[y][x] == 'A') {
				col = x;
				break;
			}
		}
		swaC(n - y + 1, col);
	}
	print();
}
ll dp[N][N];
void fillDP() {
	dp[0][0] = 1;
	for(int x = 1; x <= n; ++x) {
		dp[x][0] = dp[x-1][0]*x;
		dp[x][0] %= M;
	}
	for(ll y = 1; y <= n; ++y) {
		for(ll x = y; x <= n; ++x) {
			dp[x][y] = dp[x-1][y-1]*(x-y);
			dp[x][y] %= M;
			if(y == 1) continue;
			dp[x][y] += dp[x-1][y-2]*(y-1);
			dp[x][y] %= M;
		}
	}
}
ll exp(ll a, ll e) {
	if(e == 0) return 1;
	if(e%2 == 0)  {
		ll c = exp(a, e/2);
		return (c*c)%M;
	}
	ll c = exp(a, e-1);
	return (c*a)%M;
}
int main() {
	cin >> n;
	for(int y = 1; y <= n; ++y) {
		for(int x = 1; x <= n; ++x) {
			char c;
			cin >> c;
			t[y][x] = c;
			if(c == 'B') ++amB;
		}
	}
	sor();
	fillDP();
	ll ans = 0;
	int f4 = n - amB - aA;
	int f1 = amB;
	int a1 = 0;
	int a2 = 0;
	int a3 = 0;
	//calculate a2
	for(int y = 1; y <= n - amB - aA; ++y) {
		for(int x = 1; x <= amB; ++x) {
			if(t[y][x] == 'A') {
				++a2;
			}
		}
	}
	//calculate a3;
	for(int y = n - amB; y <= n; ++y) {
		for(int x = amB + aA + 1; x <= n; ++x) {
			if(t[y][x] == 'A') {
				++a3;
			}
		}
	}
	//calculate a1;
	for(int x = 1; x <= amB; ++x) {
		for(int y = n - amB + 1; y <= n; ++y) {
			if(t[y][x] == 'A') {
				++a1;
			}
		}
	}
	for(int f2 = max(a2, a3); f2 <= amB - a1; ++f2) {
		//Calculate the ways to pick f2 - a2 from section A2
		ll A2 = 1;
		ll fact = 1;
		int s = n - amB - aA;
		for(int j = 0; j < f2 - a2; ++j) {
			A2 *= (s - a2 - j);
			A2 %= M;
			A2 *= (amB - a2 - j);
			A2 %= M;
			fact *= (j+1);
			fact %= M;
		}
		A2 *= exp(fact, M-2);
		//Calculate in a similar manner A3
		ll A3 = 1;
		fact = 1;
		for(int j = 0; j < f2 - a3; ++j) {
			A2 *= (s - a3 - j);
			A3 %= M;
			A3 *= (amB - a3 - j);
			A2 %= M;
			fact *= (j+1);
			fact %= M;
		}
		A3 *= exp(fact, M-2);
		int f1 = amB - f2;
		int d1 = f1 - a1;
		ll A1 = dp[d1][d1];
		int f4 = n - 2*f2 - f1;
		ll A4 = 1;
		for(int i = 1; i <= f4; ++i) {
			A4 *= i;
			A4 %= M;
		}
		ll B = dp[n - amB][aA + f4];
		ll cur = A1*A2;
		cur %= M;
		cur *= A3;
		cur %= M;
		cur *= A4;
		cur %= M;
		cur *= B;
		cur %= M;
		ans += cur;
	}
	cout << ans << "\n";
}Test details
Test 1
Group: 1
Verdict: WRONG ANSWER
| input | 
|---|
| 2 .. .. | 
| correct output | 
|---|
| 2 | 
| user output | 
|---|
| .. .. ... | 
Test 2
Group: 1
Verdict: WRONG ANSWER
| input | 
|---|
| 2 .. A. | 
| correct output | 
|---|
| 1 | 
| user output | 
|---|
| .. A. ... | 
Test 3
Group: 1
Verdict: WRONG ANSWER
| input | 
|---|
| 2 B. .A | 
| correct output | 
|---|
| 0 | 
| user output | 
|---|
| .A B. ... | 
Test 4
Group: 1
Verdict: WRONG ANSWER
| input | 
|---|
| 3 ... ... ... | 
| correct output | 
|---|
| 12 | 
| user output | 
|---|
| ... ... ... ... | 
Test 5
Group: 1
Verdict: WRONG ANSWER
| input | 
|---|
| 4 .... .... .... .... | 
| correct output | 
|---|
| 216 | 
| user output | 
|---|
| .... .... .... ... | 
Test 6
Group: 1
Verdict: WRONG ANSWER
| input | 
|---|
| 5 ..... ..... ..... ..... ... | 
| correct output | 
|---|
| 5280 | 
| user output | 
|---|
| ..... ..... ..... ... | 
Test 7
Group: 1
Verdict: WRONG ANSWER
| input | 
|---|
| 5 ....A ..... ..... ..... ... | 
| correct output | 
|---|
| 264 | 
| user output | 
|---|
| ..... ..... ..... ... | 
Test 8
Group: 1
Verdict: WRONG ANSWER
| input | 
|---|
| 5 B.... ..... ..... .A.B. ... | 
| correct output | 
|---|
| 22 | 
| user output | 
|---|
| ..... ..... ..B.. ... | 
Test 9
Group: 1
Verdict: WRONG ANSWER
| input | 
|---|
| 5 B.A.. ....A ..... A.B.. ... | 
| correct output | 
|---|
| 2 | 
| user output | 
|---|
| ..... A.... .AB.. ... | 
Test 10
Group: 1
Verdict: WRONG ANSWER
| input | 
|---|
| 5 A.B.. BA... .B.A. ...BA ... | 
| correct output | 
|---|
| 1 | 
| user output | 
|---|
| ...AB ..AB. .AB.. ... | 
Test 11
Group: 2
Verdict: WRONG ANSWER
| input | 
|---|
| 10 .......... .......... .......... .......... ... | 
| correct output | 
|---|
| 306442892 | 
| user output | 
|---|
| .......... .......... .......... ... Truncated | 
Test 12
Group: 2
Verdict: WRONG ANSWER
| input | 
|---|
| 50 ................................. | 
| correct output | 
|---|
| 694861480 | 
| user output | 
|---|
| ................................. Truncated | 
Test 13
Group: 2
Verdict: WRONG ANSWER
| input | 
|---|
| 111 ................................. | 
| correct output | 
|---|
| 555319110 | 
| user output | 
|---|
| ................................. Truncated | 
Test 14
Group: 2
Verdict: WRONG ANSWER
| input | 
|---|
| 222 ................................. | 
| correct output | 
|---|
| 108372237 | 
| user output | 
|---|
| ................................. Truncated | 
Test 15
Group: 2
Verdict: WRONG ANSWER
| input | 
|---|
| 333 ................................. | 
| correct output | 
|---|
| 259107857 | 
| user output | 
|---|
| ................................. Truncated | 
Test 16
Group: 2
Verdict: WRONG ANSWER
| input | 
|---|
| 444 ................................. | 
| correct output | 
|---|
| 19906314 | 
| user output | 
|---|
| ................................. Truncated | 
Test 17
Group: 2
Verdict: WRONG ANSWER
| input | 
|---|
| 497 ................................. | 
| correct output | 
|---|
| 224313667 | 
| user output | 
|---|
| ................................. Truncated | 
Test 18
Group: 2
Verdict: WRONG ANSWER
| input | 
|---|
| 498 ................................. | 
| correct output | 
|---|
| 929574601 | 
| user output | 
|---|
| ................................. Truncated | 
Test 19
Group: 2
Verdict: WRONG ANSWER
| input | 
|---|
| 499 ................................. | 
| correct output | 
|---|
| 600226043 | 
| user output | 
|---|
| ................................. Truncated | 
Test 20
Group: 2
Verdict: WRONG ANSWER
| input | 
|---|
| 500 ................................. | 
| correct output | 
|---|
| 198353194 | 
| user output | 
|---|
| ................................. Truncated | 
Test 21
Group: 3
Verdict: WRONG ANSWER
| input | 
|---|
| 499 ................................. | 
| correct output | 
|---|
| 840243733 | 
| user output | 
|---|
| ................................. Truncated | 
Test 22
Group: 3
Verdict: WRONG ANSWER
| input | 
|---|
| 499 ........................A........ | 
| correct output | 
|---|
| 4146290 | 
| user output | 
|---|
| ................................. Truncated | 
Test 23
Group: 3
Verdict: WRONG ANSWER
| input | 
|---|
| 499 B.........A...................... | 
| correct output | 
|---|
| 173518884 | 
| user output | 
|---|
| ................................. Truncated | 
Test 24
Group: 3
Verdict: WRONG ANSWER
| input | 
|---|
| 499 ...A....B........................ | 
| correct output | 
|---|
| 20044800 | 
| user output | 
|---|
| ................................. Truncated | 
Test 25
Group: 3
Verdict: WRONG ANSWER
| input | 
|---|
| 499 AB............................... | 
| correct output | 
|---|
| 2 | 
| user output | 
|---|
| .............A................... Truncated | 
Test 26
Group: 3
Verdict: WRONG ANSWER
| input | 
|---|
| 500 ................................. | 
| correct output | 
|---|
| 121064146 | 
| user output | 
|---|
| ................................. Truncated | 
Test 27
Group: 3
Verdict: WRONG ANSWER
| input | 
|---|
| 500 ................................. | 
| correct output | 
|---|
| 848435259 | 
| user output | 
|---|
| ................................. Truncated | 
Test 28
Group: 3
Verdict: WRONG ANSWER
| input | 
|---|
| 500 .....B........A.................. | 
| correct output | 
|---|
| 296240911 | 
| user output | 
|---|
| ................................. Truncated | 
Test 29
Group: 3
Verdict: WRONG ANSWER
| input | 
|---|
| 500 .A......B........................ | 
| correct output | 
|---|
| 2196 | 
| user output | 
|---|
| .......................A......... Truncated | 
Test 30
Group: 3
Verdict: WRONG ANSWER
| input | 
|---|
| 500 ...AB............................ | 
| correct output | 
|---|
| 1 | 
| user output | 
|---|
| .............A................... Truncated | 
