#include <iostream>
#include <vector>
using namespace std;

#include <iostream>
#include <cassert>

template<long long mod>
class modint{
private:
  using T = long long;
  T a;
public:
  constexpr modint(const long long x = 0) noexcept : a((x%mod+mod)%mod) {}
  constexpr T& value() noexcept { return a; }
  constexpr const T& value() const noexcept { return a; }
  constexpr modint operator-() const noexcept {
    return modint(0) -= *this;
  }
  constexpr modint operator+(const modint& rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint& rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint& rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint& rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint& operator+=(const modint& rhs) noexcept {
    a += rhs.a;
    if(a >= mod) a -= mod;
    return *this;
  }
  constexpr modint &operator-=(const modint& rhs) noexcept {
    if(a < rhs.a) a += mod;
    a -= rhs.a;
    return *this;
  }
  constexpr modint& operator*=(const modint& rhs) noexcept {
    a = a*rhs.a%mod;
    return *this;
  }
  constexpr modint& operator/=(const modint& rhs) noexcept {
    return *this *= rhs.inv();
  }
  constexpr bool operator==(const modint& rhs) const noexcept {
    return a == rhs.a;
  }
  constexpr bool operator!=(const modint& rhs) const noexcept {
    return not (*this == rhs);
  }
  constexpr modint pow(long long k) const noexcept {
    modint ret(1);
    modint x = k > 0 ? *this : this->inv();
    k = abs(k);
    while(k > 0){
      if(k&1) ret *= x;
      x *= x;
      k >>= 1;
    }
    return ret;
  }
  constexpr modint inv() const noexcept {
    return pow(mod-2);
  }
  friend std::ostream& operator<<(std::ostream &os, const modint &X) noexcept {
    return os << X.a;
  }
  friend std::istream& operator>>(std::istream &is, modint &X) noexcept {
    is >> X.a;
    X.a %= mod;
    if(X.a < 0) X.a += mod;
    return is;
  }
};

template<typename T>
T power(T x, long long k){
  T ret{1};
  while(k > 0){
    if(k&1) ret *= x;
    x *= x;
    k /= 2;
  }
  return ret;
}

int main(){
  using mint = modint<998244353>;
  int n, m;
  cin >> n >> m;
  mint ans = 0;
  vector<mint> pow_m(n+1);
  pow_m[0] = 1;
  for(int i = 0; i < n; ++i)
    pow_m[i+1] = pow_m[i]*m;
  for(int i = 1; i <= m; ++i){
    ans += power<mint>(m-i+1,n) - power<mint>(m-i,n);
    mint a = 1, b = 1;
    for(int j = 1; j+1 <= n; ++j){
      a *= m-i+1, b *= m-i;
      if(j+2 <= n){
        mint p = a - b;// i が 1 回以上使われる
        p *= n-j-1;// 長さ j+2 の区間の取り方
        p *= (i-1)*(i-1);//両端
        p *= pow_m[n-j-2];//残り
        ans += p;
      }
      mint q = a - b;
      q *= 2;
      q *= i-1;//両端
      q *= pow_m[n-j-1];//残り
      ans += q;
    }
  }
  cout << ans << endl;
}

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int) x.size()
#define cat(x) cerr << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)

using ll = long long;
using ld = long double;
using namespace std;	

const int N = 1e5 + 505;

int n, L, a[N], b[N], d[N];
ll ans;

void solve(int l, int r) {
	if (d[l] > 0) {
		rep(i, l, r) {
			a[i] = L - a[i] + 1;
			b[i] = L - b[i] + 1;
		}
		reverse(a + l, a + r + 1);
		reverse(b + l, b + r + 1);
	}
	//~ printf ("%d -- %d\n", l, r);
	//~ rep(i, l, r)
		//~ cout << a[i] << " " << b[i] << endl;
	int j = l, i = l + 1;
	while (i <= r) {
		while (i <= r && a[j] + i - j == b[i]) {
			ans++;
			i++;
		}
		if (i == r + 1) break;
		//~ cat(i);
		//~ cat(j);
		//~ cat(a[j + 2]);
		//~ cat(b[i]);
		j++;
		while (j < i && a[j] + i - j != b[i]) j++;
		if (i == j) {
			printf ("-1\n");
			exit(0);
		} 
		while (a[j + 1] + i - (j + 1) == b[i]) j++;
		ans += i - j - 1;
	}	
	if (d[l] > 0) {
		rep(i, l, r) {
			a[i] = L - a[i] + 1;
			b[i] = L - b[i] + 1;
		}
		reverse(a + l, a + r + 1);
		reverse(b + l, b + r + 1);
	}
}

int sign(int x) {
	return (x > 0) - (x < 0);
}

int main() {
	scanf ("%d%d", &n, &L);
	a[0] = b[0] = 0;
	a[n + 1] = b[n + 1] = L + 1;
	rep(i, 1, n) scanf ("%d", a + i);
	rep(i, 1, n) scanf ("%d", b + i);
	rep(i, 1, n) d[i] = b[i] - a[i];
	for (int i = 1; i <= n;) {
		int j = i;
		while (j <= n && sign(d[i]) == sign(d[j])) j++;
		if (d[i] < 0) 
			solve(i - 1, j - 1);
		if (d[i] > 0) {
			if (d[j] < 0) return !printf ("-1\n");
			solve(i, j);
		}
		i = j;
	}
	printf ("%lld\n", ans);
	return 0;
}
