#include<bits/stdc++.h>
#define N 200005
#define re register
#define ll long long
#define P 998244353
using namespace std;
int n,m,K,q,u;
inline void Rd(int &res){
	re char c;res=0;bool f=0;
	while(c=getchar(),c<48)(c=='-')&&(f=1);
	do res=(res<<3)+(res<<1)+(c^48);
	while(c=getchar(),c>47);
	f&&(res=-res);
}
int f[25][N],len,fac[N],fac1[N];
inline void Mod(int &x){x>=P&&(x-=P);}
inline int C(int n,int m){
	if(n<m)return 0;
	return 1ll*fac[n]*fac1[m]%P*fac1[n-m]%P;
}
int Pow(int x,int k){
	int res=1;
	while(k){
		if(k&1)res=1ll*res*x%P;
		x=1ll*x*x%P;
		k>>=1;
	}
	return res;
}
int main(){
	Rd(n),Rd(m);
	for(re int i=1;i<=m;i++)f[1][i]=1;
	for(re int i=2;;i++){
		bool bo=0;
		for(re int j=1;j<=m;j++)if(f[i-1][j]){
			for(re int k=2;;k++)
			if(j*k<=m){
				bo=1;
				Mod(f[i][j*k]+=f[i-1][j]);
			}
			else break;
		}
		if(!bo){
			len=i-1;
			break;
		}
	}
	fac[0]=1;for(re int i=1;i<=n;i++)fac[i]=1ll*i*fac[i-1]%P;
	fac1[n]=Pow(fac[n],P-2);for(re int i=n-1;i>=0;i--)fac1[i]=1ll*(i+1)*fac1[i+1]%P;
	int ans=0;
	for(re int i=1;i<=len;i++){
		for(re int j=1;j<=m;j++)if(f[i][j]){
			ans=(ans+1ll*f[i][j]*C(n-1,i-1))%P;
		}
	}
	printf("%d\n",ans);
	
	return 0;
}
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
#define N 100012
inline int sgn(int x){return (x==0)?0:((x>0)?1:(-1));}
inline int Abs(int x){return (x>0)?x:(-x);}
int n,L,a[N],b[N],c[N],la[N];bool fl=true;map<int,int>mp;long long ans=0;
inline void Fin(int l,int r)
{
	if(l+1>=r)return;int i,ql=l,qr=r,ls;
	for(i=l+1;i<r-1;i++)if((c[i]==1)&&(c[i+1]==(-1))){fl=false;return;}
	for(i=l;i<=r;i++)if(c[i]==(-1))ql=i;for(i=r;i>=l;i--)if(c[i]==1)qr=i;
	mp.clear();for(i=l;i<=ql;i++){if(c[i]){if(!mp.count(b[i]-i)){fl=false;return;}la[i]=mp[b[i]-i];}mp[a[i]-i]=i;}
	mp.clear();for(i=r;i>=qr;i--){if(c[i]){if(!mp.count(b[i]-i)){fl=false;return;}la[i]=mp[b[i]-i];}mp[a[i]-i]=i;}
	ls=l;for(i=l;i<=r;i++){if(c[i]&&(ls>la[i])){fl=false;return;}if(c[i])ls=max(ls,la[i]);}
	ls=r;for(i=r;i>=l;i--){if(c[i]&&(ls<la[i])){fl=false;return;}if(c[i])ls=min(ls,la[i]);}
	ls=r;for(i=ql;i>=l;i--)if(c[i]&&(la[i]!=ls)){ans+=i-la[i];ls=la[i];}
	ls=l;for(i=qr;i<=r;i++)if(c[i]&&(la[i]!=ls)){ans+=la[i]-i;ls=la[i];}
}
int main(){
	scanf("%d%d",&n,&L);int i,l,r;for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=n;i++)scanf("%d",&b[i]);a[0]=b[0]=0;a[n+1]=b[n+1]=L+1;
	for(i=1;i<=n;i++)c[i]=sgn(b[i]-a[i]);
	for(i=0;(i<=(n+1))&&fl;i=r){l=i;r=i+1;while((r<=n)&&c[r])++r;Fin(l,r);}if(!fl){printf("-1");return 0;}printf("%lld",ans);return 0;
}