Problem:
Score : 1400 points

Problem Statement

In a two-dimensional plane, there is a square frame whose vertices are at coordinates (0,0), (N,0), (0,N), and (N,N).
The frame is made of mirror glass. A ray of light striking an edge of the frame (but not a vertex) will be reflected so that the angle of incidence is equal to the angle of reflection.
A ray of light striking a vertex of the frame will be reflected in the direction opposite to the direction it is coming from.

We will define the path for a grid point (a point with integer coordinates) (i,j) (0<i,j<N) strictly within the frame, as follows:

The path for (i,j) is the union of the trajectories of four rays of light emitted from (i,j) to (i-1,j-1), (i-1,j+1), (i+1,j-1), and (i+1,j+1).

Figure: an example of a path for a grid point

There is a light bulb at each grid point strictly within the frame. We will assign a state - ON or OFF - to each bulb. The state of the whole set of bulbs are called beautiful if it is possible to turn OFF all the bulbs by repeating the following operation:

Choose a grid point strictly within the frame, and switch the states of all the bulbs on its path.

Takahashi has set the states of some of the bulbs, but not for the remaining bulbs.
Find the number of ways to set the states of the remaining bulbs so that the state of the whole set of bulbs is beautiful, modulo 998244353.
The state of the bulb at the grid point (i,j) is set to be ON if A_{i,j}=o, OFF if A_{i,j}=x, and unset if A_{i,j}=?.

Constraints

2 \leq N \leq 1500

A_{ij} is o, x, or ?.

Input

Input is given from Standard Input in the following format:

N
A_{1,1}...A_{1,N-1}
:
A_{N-1,1}...A_{N-1,N-1}

Output

Print the answer.

Sample Input 1

4
o?o
???
?x?

Sample Output 1

1

The state of the whole set of bulbs will be beautiful if we set the state of each bulb as follows:

oxo
xox
oxo

We can turn OFF all the bulbs by, for example, choosing the point (1, 1) and switching the states of the bulbs at (1,1), (1,3), (2,2), (3,1), and (3,3).

Sample Input 2

5
o?o?
????
o?x?
????

Sample Output 2

0

Sample Input 3

6
?o???
????o
??x??
o????
???o?

Sample Output 3

32

Sample Input 4

9
????o??x
?????x??
??o?o???
?o?x????
???????x
x?o?o???
????????
x?????x?

Sample Output 4

4

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

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "functional"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<string>s(N - 1);
	for (auto &i : s)cin >> i;
	reverse(s.begin(), s.end());
	vector<vector<int>>Xmaru(2 * N);
	vector<int>Xques(2 * N);
	vector<int>Xbatu(2 * N);
	vector<int>Ymaru(2 * N);
	vector<int>Yques(2 * N);
	vector<int>Ybatu(2 * N);
	vector<vector<int>>batu(N * 2, vector<int>(N * 2));
	vector<vector<int>>maru(N * 2, vector<int>(N * 2));
	for (int i = 1; i < N; i++) {
		for (int j = 1; j < N; j++) {
			if (s[i - 1][j - 1] == 'o') {
				//cout << i + j << endl;
				Xmaru[i + j].push_back(i - j + N);
				Ymaru[i - j + N]++;
				maru[i - j + N][i + j]++;
			}
			else if (s[i - 1][j - 1] == 'x') {
				Xbatu[i + j]++;
				Ybatu[i - j + N]++;
				batu[i - j + N][i + j]++;
			}
			else {
				Xques[i + j]++;
				Yques[i - j + N]++;
			}
		}
	}
	long long int ans = 0;
	int mx = 0;
	for (int i = 2; i < N * 2 - 1; i++) {
		if (mx < Xmaru[i].size())mx = Xmaru[i].size();
	}
	if (mx > 2) {
		vector<int>chooseX;
		for (int i = 2; i < N * 2 - 1; i++) {
			if (Xmaru[i].size() > 2)chooseX.push_back(i);
		}
		if (chooseX.size() > 2) {
			cout << 0 << endl;
			return 0;
		}
		if (chooseX.size() == 2) {
			if (chooseX[0] + chooseX[1] != 2 * N) {
				cout << 0 << endl;
				return 0;
			}
			for (auto i : chooseX) {
				for (auto j : Xmaru[i]) {
					Ymaru[j]--;
					if (batu[j][i]) {
						cout << 0 << endl;
						return 0;
					}
				}
			}
			int num = 0;
			int chooseY = 0;
			for (int i = 2; i <= N; i++) {
				if ((Ymaru[i] && Ybatu[i]) || (Ymaru[2 * N - i] && Ybatu[2 * N - i])) {
					cout << 0 << endl;
					return 0;
				}
				if (Ymaru[i] || Ymaru[2 * N - i])chooseY++;
				else if (!Ybatu[i] && !Ybatu[2 * N - i])num++;
			}
			if (chooseY > 1) {
				cout << 0 << endl;
				return 0;
			}
			if (chooseY == 1) {
				cout << 1 << endl;
				return 0;
			}
			else {
				cout << num << endl;
				return 0;
			}
		}
		else if (chooseX.size() == 1) {
			for (auto i : chooseX) {
				for (auto j : Xmaru[i]) {
					Ymaru[j]--;
					if (batu[j][i]) {
						cout << 0 << endl;
						return 0;
					}
				}
			}
			long long int ans = 0;
			for (int i = 2; i < N * 2 - 1; i++) {
				if (i + chooseX.front() != 2 * N)continue;
				for (auto j : Xmaru[i]) {
					Ymaru[j]--;
					if (batu[j][i]) {
						cout << 0 << endl;
						return 0;
					}
				}
				bool flag = true;
				int num = 0;
				int chooseY = 0;
				for (int j = 2; j <= N; j++) {
					if ((Ymaru[j] && Ybatu[j]) || (Ymaru[2 * N - j] && Ybatu[2 * N - j])) {
						flag = false;
						break;
					}
					if (Ymaru[j] || Ymaru[2 * N - j])chooseY++;
					else if (!Ybatu[j] && !Ybatu[2 * N - j])num++;
				}
				//cout << "flag " << flag << endl;
				//cout << "chooseY " << chooseY << endl;
				//cout << "num " << num << endl;
				if (flag) {
					if (chooseY > 1) {
						ans += 0;
					}
					if (chooseY == 1) {
						ans++;
					}
					else {
						ans += num;
					}
				}
				cout << ans << endl;
				for (auto j : Xmaru[i])Ymaru[j]++;
			}
			ans %= MOD;
			//cout << ans << endl;
		}
	}
	for (int i = 2; i <= N; i++) {
		if (Xbatu[i] || Xbatu[2 * N - i])continue;
		bool con = false;
		for (auto j : Xmaru[i]) {
			Ymaru[j]--;
			if (batu[j][i]) {
				con = true;
			}
		}
		for (auto j : Xmaru[2 * N - i]) {
			Ymaru[j]--;
			if (batu[j][i]) {
				con = true;
			}
		}
		if (con)continue;
		bool flag = true;
		int num = 0;
		int chooseY = 0;
		for (int j = 2; j <= N; j++) {
			if ((Ymaru[j] && Ybatu[j]) || (Ymaru[2 * N - j] && Ybatu[2 * N - j])) {
				flag = false;
				break;
			}
			if (Ymaru[j] || Ymaru[2 * N - j])chooseY++;
			else if (!Ybatu[j] && !Ybatu[2 * N - j])num++;
		}
		if (flag) {
			if (chooseY > 1) {
				ans += 0;
			}
			if (chooseY == 1) {
				ans++;
			}
			else {
				ans += num;
			}
		}
	//	cout << ans << endl;
		for (auto j : Xmaru[i]) {
			Ymaru[j]++;
			if (batu[j][i]) {
				cout << 0 << endl;
				return 0;
			}
		}
		for (auto j : Xmaru[2 * N - i]) {
			Ymaru[j]++;
			if (batu[j][i]) {
				cout << 0 << endl;
				return 0;
			}
		}
	}
	ans %= MOD;
	cout << ans << endl;
	return 0;
}