Definition of f [i] [j] represents the (i, j) till the end of the desired line, from the bottom upward and dp then for each row can be obtained m equations.
But because the m equations instead of the DAG , so consider using Gaussian elimination, but not time complexity.
If the observation equation can be found constantly f [i] [j] is shown f [i] [j + 1 ] and substituting it can calculate F [n-] [m] , then it can continue substituting trans.
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define mod 998244353 4 #define ll long long 5 ll n,m,a,b,x[1001],y[1001],f[1001]; 6 ll ksm(ll n,ll m){ 7 if (!m)return 1; 8 ll s=ksm(n,m>>1); 9 s=s*s%mod; 10 if (m&1)s=s*n%mod; 11 return s; 12 } 13 int main(){ 14 scanf("%lld%lld%lld%lld",&n,&m,&a,&b); 15 if (m==1){ 16 printf("%lld",2*(n-a)); 17 return 0; 18 } 19 for(int i=1;i<=n-a;i++){ 20 x[1]=(mod+1)/2; 21 y[1]=(f[1]+3)*x[1]%mod; 22 for(int j=2;j<m;j++){ 23 x[j]=(3LL*mod+1)/4; 24 y[j]=(1+x[j]*(f[j]+y[j-1]))%mod; 25 x[j]=ksm(x[j]*(3-x[j-1]+mod)%mod,mod-2); 26 y[j]=y[j]*x[j]%mod; 27 x[j]=x[j]*(3LL*mod+1)/4%mod; 28 } 29 f[m]=(y[m-1]+f[m]+3)*ksm((2-x[m-1]+mod)%mod,mod-2)%mod; 30 for(int j=m-1;j;j--)f[j]=(x[j]*f[j+1]+y[j])%mod; 31 } 32 printf("%lld",f[b]); 33 return 0; 34 }