Source Code
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.function.Predicate;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author dauom
 */
public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskI solver = new TaskI();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskI {
    static ModArithmetic mod = new ModArithmetic(998244353, true);
    static int[][] fif = mod.getFactorialAndInverse(5005);
    char[] s;
    Integer[][][] dp;

    public final void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      int m = in.nextInt();
      char[] a = in.nextCharArray();
      s = in.nextCharArray();
      int o = ArrayCounter.countMatching(a, c -> c == '1');
      int z = ArrayCounter.countMatching(a, c -> c == '0');
      dp = new Integer[s.length][z + 1][o + 1];
      out.println(mod.div(f(0, z, o), ways(z, o)));
    }

    public final int f(int i, int z, int o) {
      if (z == 0 && o == 0 && i < s.length) {
        return 0;
      }
      if (i >= s.length) {
        return ways(z, o);
      }
      if (dp[i][z][o] != null) {
        return dp[i][z][o];
      }

      int ways = 0;
      if (s[i] == '0') {
        if (z > 0) {
          ways = f(i + 1, z - 1, o);
        }
        if (o > 0) {
          ways = mod.add(ways, f(i, z, o - 1));
        }
      } else {
        if (z > 0) {
          ways = f(i, z - 1, o);
        }
        if (o > 0) {
          ways = mod.add(ways, f(i + 1, z, o - 1));
        }
      }
      return dp[i][z][o] = ways;
    }

    private int ways(int z, int o) {
      return mod.mul(fif[0][z + o], mod.mul(fif[1][z], fif[1][o]));
    }

  }

  static class ArrayCounter {
    private ArrayCounter() {
      throw new RuntimeException("DON'T");
    }

    public static int countMatching(char[] arr, Predicate<Character> test, int from, int to) {
      int ret = 0;
      for (int i = from; i <= to; i++) {
        if (test.test(arr[i])) {
          ++ret;
        }
      }
      return ret;
    }

    public static int countMatching(char[] arr, Predicate<Character> test) {
      return countMatching(arr, test, 0, arr.length - 1);
    }

  }

  static final class ModArithmetic {
    public static final int DEFAULT_MOD = 1_000_000_007;
    public final int m;
    public final boolean isPrime;

    public ModArithmetic() {
      this(DEFAULT_MOD, true);
    }

    public ModArithmetic(int mod) {
      this(mod, false);
    }

    public ModArithmetic(int mod, boolean isPrime) {
      if (mod <= 0) {
        throw new IllegalArgumentException("Modulo must be > 0");
      }
      this.m = mod;
      this.isPrime = isPrime;
    }

    public final int add(int a, int b) {
      a += b;
      if (a >= m) {
        a -= m;
      }
      return a;
    }

    public final int mul(int a, int b) {
      long m = a * (long) b;
      if (m >= this.m) {
        m %= this.m;
      }
      return (int) m;
    }

    public final int div(int a, int b) {
      if (isPrime) {
        return mul(a, pow(b, m - 2));
      }
      throw new UnsupportedOperationException();
    }

    public final int pow(long a, long b) {
      long res = 1;
      while (b > 0) {
        if ((b & 1) == 1) {
          res *= a;
          if (res >= m) {
            res %= m;
          }
        }
        b >>= 1;
        a *= a;
        if (a >= m) {
          a %= m;
        }
      }
      return (int) res;
    }

    public final int[][] getFactorialAndInverse(int n) {
      int[] f = new int[n + 1];
      int[] fi = new int[n + 1];
      f[0] = 1;
      for (int i = 1; i <= n; i++) {
        f[i] = mul(f[i - 1], i);
      }
      fi[n] = div(1, f[n]);
      for (int i = n - 1; i >= 0; i--) {
        fi[i] = mul(fi[i + 1], i + 1);
      }
      return new int[][]{f, fi};
    }

    public String toString() {
      return "ModArithmetic{" + "m=" + m + ", isPrime=" + isPrime + '}';
    }

  }

  static final class InputReader {
    private final InputStream stream;
    private final byte[] buf = new byte[1 << 18];
    private int curChar;
    private int numChars;

    public InputReader() {
      this.stream = System.in;
    }

    public InputReader(final InputStream stream) {
      this.stream = stream;
    }

    private int read() {
      if (this.numChars == -1) {
        throw new UnknownError();
      } else {
        if (this.curChar >= this.numChars) {
          this.curChar = 0;

          try {
            this.numChars = this.stream.read(this.buf);
          } catch (IOException ex) {
            throw new InputMismatchException();
          }

          if (this.numChars <= 0) {
            return -1;
          }
        }

        return this.buf[this.curChar++];
      }
    }

    public final int nextInt() {
      int c;
      for (c = this.read(); isSpaceChar(c); c = this.read()) {
      }

      byte sgn = 1;
      if (c == 45) { // 45 == '-'
        sgn = -1;
        c = this.read();
      }

      int res = 0;

      while (c >= 48 && c <= 57) { // 48 == '0', 57 == '9'
        res *= 10;
        res += c - 48; // 48 == '0'
        c = this.read();
        if (isSpaceChar(c)) {
          return res * sgn;
        }
      }

      throw new InputMismatchException();
    }

    public final String next() {
      int c;
      while (isSpaceChar(c = this.read())) {
      }

      StringBuilder result = new StringBuilder();
      result.appendCodePoint(c);

      while (!isSpaceChar(c = this.read())) {
        result.appendCodePoint(c);
      }

      return result.toString();
    }

    private static boolean isSpaceChar(final int c) {
      return c == 32 || c == 10 || c == 13 || c == 9
          || c == -1; // 32 == ' ', 10 == '\n', 13 == '\r', 9 == '\t'
    }

    public final char[] nextCharArray() {
      return next().toCharArray();
    }

  }
}

Copy
Binary Stack Easy dauom
Java 11
645 ms
262.1 MB
Memory Limit Exceeded