#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\acm\\Cproj\\in.txt","r",stdin);
#define pr freopen("C:\\acm\\Cproj\\out.txt","w",stdout)
#define pr2 freopen("C:\\acm\\Cproj\\std.txt","w",stdout)
#define pii pair<int,int>
#define Please return
#define AC 0
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T>
void read(T &val) { T x = 0; T bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; }
const int mod=998244353;
const int maxn = 1e6+10;

int n,l,a[maxn],q,t,b[maxn];
char s[maxn];
vector<pii>g[maxn];

int main(){
//    op;
    read(n);read(l);
    unordered_map<int,int>lef,rig;
    fo(i,1,n) {
        read(a[i]);
    }
    fo(i,1,n) read(b[i]);
    lef[0]=0;rig[l-n]=n+1;
    LL ans = 0;
    fo(i,1,n){//往左的
        if(b[i]>a[i]) continue;
        if(a[i]!=b[i]&&!lef.count(b[i]-i)) {
            puts("-1");return 0;
        }
        int tar = lef[b[i]-i];
        lef[a[i]-i]=i;
        if(a[i]==b[i]) continue;
        lef[b[i]-i]=i;
        g[i].push_back({tar,abs(i-tar)});//表示i小于tar，也就是i在tar之前就得要行动
        ans+=abs(i-tar);
    }

    fod(i,n,1){
        if(b[i]<a[i]) continue;
        if(a[i]!=b[i]&&!rig.count(b[i]-i)) {
            puts("-1");return 0;
        }
        int tar = rig[b[i]-i];
        rig[a[i]-i]=i;
        if(a[i]==b[i]) continue;
        rig[b[i]-i]=i;
        g[i].push_back({tar,abs(i-tar)});
        ans+=abs(i-tar);
    }

    printf("%lld\n",ans);



    Please AC;
}
#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector <int>
#define vl vector <ll>
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define ff first
#define ss second
#define MOD 1000000007

const ll mod = 1e9 + 7;

ll power(ll a, ll b) { //a^b
	ll res = 1;
	a = a % MOD;
	while (b > 0) {
		if (b & 1) {res = (res * a) % MOD; b--;}
		a = (a * a) % MOD;
		b >>= 1;
	}
	return res;
}

ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}

int main() {
#ifndef ONLINE_JUDGE
	// for getting input from input.txt
	freopen("input.txt", "r", stdin);
	// for writing output to output.txt
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int h, w;
	cin >> h >> w;
	vector <string> v(h);
	for (int i = 0; i < h; i++)
		cin >> v[i];
	ll ans = 0;
	vector <vl> prer(h, vl(w, -1));
	vector <vl> suffr(h, vl(w, h));
	vector <vl> prec(h, vl(w, -1));
	vector <vl> suffc(h, vl(w, w));
	//cout << prec[0] << " " << prec[1] << "\n";
	ll c = 0;
	for (ll i = 0; i < h; i++) {
		for (ll j = 0; j < w; j++) {
			if (v[i][j] == '#')
				prec[i][j] = j;
			else
				c++;
			if (j)
				prec[i][j] = max(prec[i][j], prec[i][j - 1]);
			//cout << j << " " << prec[j] << "\n";
		}
		for (ll j = w - 1; j >= 0; j--) {
			if (v[i][j] == '#')
				suffc[i][j] = j;
			if (j + 1 < w)
				suffc[i][j] = min(suffc[i][j], suffc[i][j + 1]);
		}
	}
	//cout << prec[0] << " " << prec[1] << "f\n";
	for (ll j = 0; j < w; j++) {
		for (ll i = 0; i < h; i++) {
			if (v[i][j] == '#')
				prer[i][j] = i;
			if (i)
				prer[i][j] = max(prer[i][j], prer[i - 1][j]);
		}
		for (ll i = h - 1; i >= 0; i--) {
			if (v[i][j] == '#')
				suffr[i][j] = i;
			if (i + 1 < h)
				suffr[i][j] = min(suffr[i][j], suffr[i + 1][j]);
		}
	}
	//cout << prec[0] << " " << prec[1] << "\n";
	ll tot = power(2, c);
	//cout << tot << "\n";
	for (ll i = 0; i < h; i++) {
		for (ll j = 0; j < w; j++) {
			//cnt = 0;
			if (v[i][j] == '#')
				continue;
			ll cnt = 0;
			if (i)
				cnt += i - 1 - prer[i - 1][j];
			if (i + 1 < h)
				cnt += suffr[i + 1][j] - (i + 1);
			if (j)
				cnt += j - 1 - prec[i][j - 1];
			if (j + 1 < w)
				cnt += suffc[i][j + 1] - (j + 1);
			cnt++;
			//cout << cnt << " " << i << " " << j << "\n";
			ll no = tot - power(2, c - cnt);
			no = (no + mod) % mod;
			//cout << no << "\n";
			ans = (ans + no) % mod;
		}
	}
	cout << ans << "\n";
}
