#include #include #include #include #define mod 998244353 #define N 200005 using namespace std; typedef long long ll; ll cas,n,i,pp,last,now,Mi,mx,ans,premi,premx,j; ll mi[N],ni[N],a[N],b[N],flag[N],ok[N]; ll C(ll x,ll y) { if (y==0||x==y)return 1; return mi[x]*ni[y]%mod*ni[x-y]%mod; } int main() { mi[0]=ni[0]=ni[1]=1; for (i=1;ib[i]) { pp=1; break; } for (i=2;i<=n;i++) if (a[i-1]b[i]) { pp=1; break; } //printf("pp %I64d\n",pp); if (pp){puts("0");continue;} for (i=1;i<=n;i++)flag[i]=0,ok[i]=0; for (i=1;i<=n;i++) if (a[i]!=a[i-1]) { flag[a[i]]=1; ok[i]=a[i]; } for (i=1;i<=n;i++) if (b[i]!=b[i-1]) { flag[b[i]]=1; ok[i]=b[i]; } //for (i=1;i<=n;i++)printf("%I64d ",ok[i]);puts(""); now=0;i=1;Mi=n+10;mx=0;last=0; premi=n+10;premx=0;ans=1; while (i<=n) { if (ok[i]) { Mi=min(Mi,ok[i]); mx=max(mx,ok[i]); last=i;i++; continue; } if (premx==0) { for (j=Mi;j<=mx;j++) if (!flag[j])now++; premx=mx;premi=Mi; }else { for (j=premx+1;j<=mx;j++) if (!flag[j])now++; for (j=Mi;j<=premi-1;j++) if (!flag[j])now++; premx=mx;premi=Mi; } //printf("%I64d %I64d %I64d\n",now,last,i); while (!ok[i] && i<=n)i++; ans=ans*C(now,i-last-1)%mod*mi[i-last-1]%mod; now-=i-last-1; } printf("%I64d\n",ans); } }