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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    private FastScanner in;
    private PrintWriter out;

    final int mod = 998244353;

    int add(int x, int y) {
        x += y;
        if (x >= mod) {
            x -= mod;
        }
        return x;
    }

    int mul(int x, int y) {
        return (int) (x * 1L * y % mod);
    }

    int[] fact;
    int[] factInv;


    int solve(int leftCnt, int rightCnt, int middle) {
        if (middle == 0) {
            if (leftCnt == 0 && rightCnt == 0) {
                return 1;
            }
            return 0;
        }
        return c(leftCnt + rightCnt + middle - 1, middle - 1);
    }

    int countOnes(String s) {
        int res = 0;
        for (char c : s.toCharArray()) {
            if (c == '1') {
                res++;
            }
        }
        return res;
    }

    int solveOneWay(String A, String B, String C, String D) {
        boolean atleastOne = false;
        int res = 0;
        int cntC = countOnes(C), cntD = countOnes(D);

        int[] dp = new int[A.length() + 1];
        int curA = 0, curB = 0;
        for (int i = A.length() - 1; i >= 0; i--) {
            if (A.charAt(i) == '0' && B.charAt(i) == '0') {
                dp[i] = dp[i + 1];
                continue;
            }
            atleastOne = true;
            dp[i] = dp[i + 1];
            int mul = 1;
            if (A.charAt(i) == '1' && B.charAt(i) == '1') {
                mul = 2;
            }
            dp[i] = add(dp[i], solve(curA, curB, cntD));
            dp[i] = mul(dp[i], mul);
            curA += (A.charAt(i) == '1' ? 1 : 0);
            curB += (B.charAt(i) == '1' ? 1 : 0);
        }

        curA = 0;
        curB  = 0;
        for (int i = 0; i < A.length(); i++) {
            if (A.charAt(i) == '1' || B.charAt(i) == '1') {
                int f = solve(curA, curB, cntC);
                res = add(res, mul(dp[i], f));


                if (A.charAt(i) == '1') {
                    curA++;
                }
                if (B.charAt(i) == '1') {
                    curB++;
                }
            }
        }
        return atleastOne ? res : -1;
    }

    int solve(String A, String B, String C, String D) {
        int r1 = solveOneWay(A, B, C, D);
        int r2 = solveOneWay(C, D, A, B);
        if (r1 == -1 && r2 == -1) {
            return 1;
        }
        return add(Math.max(r1, 0), Math.max(r2, 0));
    }

    final int MAX = (int) 3e5 + 10;

    int[] fact(int n) {
        int[] res = new int[n];
        res[0] = 1;
        for (int i = 1; i < n; i++) {
            res[i] = mul(res[i - 1], i);
        }
        return res;
    }

    int[] factInv(int[] fact) {
        int n = fact.length;
        int[] res = new int[n];
        res[n - 1] = BigInteger.valueOf(fact[n - 1]).modInverse(BigInteger.valueOf(mod)).intValue();
        for (int i = n - 2; i >= 0; i--) {
            res[i] = mul(res[i + 1], i + 1);
        }
        return res;
    }

    int c(int n, int k) {
        if (k < 0 || k > n) {
            return 0;
        }
        return mul(fact[n], mul(factInv[k], factInv[n - k]));
    }

    private void solve() {
        fact = fact(MAX);
        factInv = factInv(fact);
        int n = in.nextInt();
        int m = in.nextInt();
        String A = in.next();  // len n
        String B = in.next();
        String C = in.next();
        String D = in.next();
        out.println(solve(A, B, C, D));
    }

    private void run() {
        try {
            in = new FastScanner(new File("CodeFestival_QualA_E.in"));
            out = new PrintWriter(new File("CodeFestival_QualA_E.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void runIO() {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    private class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    public static void main(String[] args) {
        new Main().runIO();
    }
}