#include<bits/stdc++.h> using namespace std; #define N 420000 #define LL long long const LL mod=998244353; LL n,m,ans,pw[N],f[N],g[N],fac[N],ifac[N]; char a[N],b[N],c[N],d[N]; bool chk; LL inv(LL x){return x==1?1:(mod-mod/x)*inv(mod%x)%mod;} LL C(LL x,LL y){if (y<0){if (x<0) return 1; return 0;} return fac[x]*ifac[y]%mod*ifac[x-y]%mod;} void solve(LL n,LL m,char a[],char b[],char c[],char d[]){ memset(f,0,sizeof f); memset(g,0,sizeof g); LL l=0,r=0,sum=0,sum2=0,cnt=0,cnt2=0,tmp=0; for (LL i=1;i<=m;i++){ if (c[i]=='1') l++; if (d[i]=='1') r++; } for (LL i=1;i<=n;i++){ if (a[i]=='1') sum++; if (b[i]=='1') sum++; if (a[i]=='1' && b[i]=='1') sum2++; } if (a[1]=='1'||b[1]=='1') f[0]=1; if (a[n]=='1'||b[n]=='1') tmp=g[n+1]=pw[sum2]; for (LL i=1;i<=n;i++){ if (i>1&&(a[i-1]=='1'||b[i-1]=='1')) g[i]=C(sum-cnt+r-1,r-1)*pw[cnt2]%mod; tmp=(tmp+g[i])%mod; if (a[i]=='1') cnt++; if (b[i]=='1') cnt++; if (a[i]=='1' && b[i]=='1') cnt2++; if (i<n&&(a[i+1]=='1'||b[i+1]=='1')) f[i]=C(cnt+l-1,l-1)*inv(pw[cnt2])%mod; } for (LL i=1;i<=n;i++){ tmp=(tmp-g[i]+mod)%mod; ans=(ans+f[i-1]*tmp)%mod; } } int main(){ scanf("%lld%lld",&n,&m); scanf("%s%s%s%s",a+1,b+1,c+1,d+1); for (int i=1;i<=n;i++) chk|=(a[i]=='1')|(b[i]=='1'); for (int i=1;i<=m;i++) chk|=(c[i]=='1')|(d[i]=='1'); if (!chk){puts("1"); return 0;} pw[0]=1; for (LL i=1;i<N;i++) pw[i]=pw[i-1]*2%mod; fac[0]=1; for (LL i=1;i<N;i++) fac[i]=fac[i-1]*i%mod; ifac[N-1]=inv(fac[N-1]); for (LL i=N-2;i>=0;i--) ifac[i]=ifac[i+1]*(i+1)%mod; solve(n,m,a,b,c,d); solve(m,n,c,d,a,b); printf("%lld\n",ans); return 0; }
|