Problem:
Score : 1600 points

Problem Statement

Ringo got interested in modern art. He decided to draw a big picture on the board with N+2 rows and M+2 columns of squares constructed in the venue of CODE FESTIVAL 2017, using some people.

The square at the (i+1)-th row and (j+1)-th column in the board is represented by the pair of integers (i,j). That is, the top-left square is (0,0), and the bottom-right square is (N+1,M+1).
Initially, the squares (x,y) satisfying 1 \leq x \leq N and 1 \leq y \leq M are painted white, and the other (outermost) squares are painted black.

Ringo arranged people at some of the outermost squares, facing inward.
More specifically, the arrangement of people is represented by four strings A, B, C and D, as follows:

For each row except the top and bottom, if the i-th character (1 \leq i \leq N) in A is 1, place a person facing right at the square (i,0); otherwise, do nothing.

For each row except the top and bottom, if the i-th character (1 \leq i \leq N) in B is 1, place a person facing left at the square (i,M+1); otherwise, do nothing.

For each column except the leftmost and rightmost, if the i-th character (1 \leq i \leq M) in C is 1, place a person facing down at the square (0,i); otherwise, do nothing.

For each column except the leftmost and rightmost, if the i-th character (1 \leq i \leq M) in D is 1, place a person facing up at the square (N+1,i); otherwise, do nothing.

Each person has a sufficient amount of non-white paint. No two people have paint of the same color.

An example of an arrangement of people (For convenience, black squares are displayed in gray)

Ringo repeats the following sequence of operations until all people are dismissed from the venue.

Select a person who is still in the venue.

The selected person repeats the following action while the square in front of him/her is white: move one square forward, and paint the square he/she enters with his/her paint. When the square in front of him/her is not white, he/she stops doing the action.

The person is now dismissed from the venue.

An example of a way the board is painted

How many different states of the board can Ringo obtain at the end of the process? Find the count modulo 998244353.

Two states of the board are considered different when there is a square painted in different colors.

Constraints

1 \leq N,M \leq 10^5

|A|=|B|=N

|C|=|D|=M

A, B, C and D consist of 0 and 1.

Input

Input is given from Standard Input in the following format:

N M
A
B
C
D

Output

Print the number of the different states of the board Ringo can obtain at the end of the process, modulo 998244353.

Sample Input 1

2 2
10
01
10
01

Sample Output 1

6

There are six possible states as shown below.

Sample Input 2

2 2
11
11
11
11

Sample Output 2

32

Sample Input 3

3 4
111
111
1111
1111

Sample Output 3

1276

Sample Input 4

17 21
11001010101011101
11001010011010111
111010101110101111100
011010110110101000111

Sample Output 4

548356548

Be sure to find the count modulo 998244353.

Sample Input 5

3 4
000
101
1111
0010

Sample Output 5

21

Sample Input 6

9 13
111100001
010101011
0000000000000
1010111111101

Sample Output 6

177856

Sample Input 7

23 30
01010010101010010001110
11010100100100101010101
000101001001010010101010101101
101001000100101001010010101000

Sample Output 7

734524988

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

#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); i--)
#define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int) a.size()
#define ALL(a) a.begin(),a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const LL INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MOD = 998244353;

const int MAX = 300300;

LL bpow(LL a, LL b)
{
	LL res = 1;
	while(b)
	{
		if (b & 1) res = (res * a) % MOD;
		a = (a * a) % MOD;
		b /= 2;
	}
	return res;
}

LL inv(LL a)
{
	return bpow(a, MOD - 2);
}

LL F[MAX];
LL IF[MAX];

LL C(int n, int k)
{
	LL res = F[n];
	res *= IF[k];
	res %= MOD;
	res *= IF[n-k];
	res %= MOD;
	return res;
}

char A[4][MAX];
int n, m;

LL V[2][MAX];

void calcVals(int x, char* u, char* d, int m, LL* V)
{
	int y = 0;
	int z = 0;
	FOR (i, 0, m)
	{
		y += u[i];
		z += d[i];

		if (x == 0)
		{
			if (y == 0 && z == 0) V[i] = 1;
			else V[i] = 0;
		}
		else
		{
			V[i] = C(x + y + z - 1, x - 1);
		}
	}
}

LL solve(char* u, char* d, char* l, char* r)
{
	int x = 0;
	FOR (i, 0, n)
	{
		x += l[i];
	}

	calcVals(x, u, d, m, V[0]);

	x = 0;
	FOR (i, 0, n)
	{
		x += r[i];
	}

	reverse(d, d+m);
	reverse(u, u+m);

	calcVals(x, u, d, m, V[1]);

	reverse(d, d+m);
	reverse(u, u+m);
	reverse(V[1], V[1] + m);

	LL res = 0;
	LL sum = 0;

	FOR (i, 0, m)
	{

		if (i) sum += V[0][i-1];
		else
		{
			sum++;
		}
		sum = (sum * (u[i] + d[i])) % MOD;
		LL cur = 0;
		if (i < m - 1) cur = V[1][i+1];
		else cur = 1;

	//	cout<<i<<": "<<sum<<' '<<cur<<endl;

		cur *= sum;

		res = (res + cur) % MOD;
	}

	return res;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//ios::sync_with_stdio(false); cin.tie(0);

	F[0] = 1;
	IF[0] = 1;
	FOR (i, 1, MAX)
	{
		F[i] = (F[i-1] * i) % MOD;
		IF[i] = inv(F[i]);
	}

	scanf("%d%d", &n, &m);

	FOR (it, 0, 4)
	{
		scanf("%s", A[it]);
		int len = strlen(A[it]);
		FOR (j, 0, len)
		{
			A[it][j] -= '0';
		}
	}

	char* l = A[0];
	char* r = A[1];
	char* u = A[2];
	char* d = A[3];

	int ind = 0;
	FOR (i, 0, m)
	{
		if (u[i] == 0 && d[i] == 0) continue;
		u[ind] = u[i];
		d[ind] = d[i];
		ind++;
	}

	m = ind;

	ind = 0;
	FOR (i, 0, n)
	{
		if (l[i] == 0 && r[i] == 0) continue;
		l[ind] = l[i];
		r[ind] = r[i];
		ind++;
	}

	n = ind;

	//cout<<"!! "<<n<<' '<<m<<endl;

	if (n == 0 || m == 0)
	{
		LL res = 1;
		FOR (i, 0, m)
		{
			res = (res * (d[i] + u[i])) % MOD;
		}
		FOR (i, 0, n)
		{
			res = (res * (l[i] + r[i])) % MOD;
		}

		cout<<res<<endl;
		return 0;
	}

	LL res = solve(u, d, l, r);
	swap(n, m);
	res += solve(l, r, u, d);

	res %= MOD;
	cout<<res<<endl;



}