#pragma region
#ifdef LOCAL
#include "pch.h"
#else
#include <bits/stdc++.h>
#endif

// debug
template<typename ... Types>
void __read(Types& ... args) {
  ((std::cin >> args), ...);
}
#define read(type, ...) type  __VA_ARGS__; __read(__VA_ARGS__)

template <typename Type, typename... Types>
auto print(Type const &arg, Types const &... args) {
  std::cout << arg, ((std::cout << " " << args), ...), std::cout << std::endl;
}
template <typename Type, typename... Types>
auto print_debug(Type const &arg, Types const &... args) {
  std::cout << arg, ((std::cout << " | " << args), ...), std::cout << std::endl;
}
#ifdef LOCAL
#define debug(...) do{std::cout <<  __LINE__ << ": [" << #__VA_ARGS__ << "]: ", print_debug(__VA_ARGS__);}while(0)
#else
#define debug(...) 1
#endif

#define __DEF_PRINTER(container)                                          \
template<typename T>                                                      \
std::ostream& operator<<(std::ostream& out, container<T> const& a)        \
{                                                                         \
  size_t len = a.size();                                                  \
  for(auto x : a) out << x << " \n"[len--==0];                            \
  return out;                                                             \
}

__DEF_PRINTER(std::vector)
__DEF_PRINTER(std::set)
__DEF_PRINTER(std::unordered_set)
// end debug

#define ALL(container) std::begin(container), std::end(container)
#define RANGE(container, lo, hi) std::begin(container)+(lo), std::begin(container)+(hi)

const char nl = '\n';
using std::cout;
using std::cin;

template <typename T>
void minmax(T k1, T k2, T& min, T& max) {
  min = std::min(k1, k2);
  max = std::max(k1, k2);
}

std::vector<int> iota(int size) {
  std::vector<int> res(size);
  std::iota(res.begin(), res.end(), 0);
  return res;
}

template <typename Iter>
auto counter(Iter from, Iter to) {
  std::map<typename Iter::value_type, int> res;
  while(from != to) res[*from++]++;
  return res;
}

const long long mod = 1e9+7;
long long reduce_mod(long long a) {
  return (a % mod + mod) % mod;
}
long long add(long long a, long long b) {
  return reduce_mod(a + b);
}
long long mult(long long a, long long b) {
  return reduce_mod(a * b);
}

std::string yn(bool yes) {
  return yes ? "yes" : "no";
}

#pragma endregion

int main() {
  read(int, n);
  int min = -1;
  int max = -1;
  for (int i = 0; i < n; i++) {
    read(int, b);
    // debug(b);
    max = max == -1 ? b : std::max(max, b);
  }
  for (int i = 0; i < n; i++) {
    read(int, a);
    // debug(a);
    min = min == -1 ? a : std::min(min, a);
  }
  // debug(min, max);
  if (min < max) {
    print(0);
  } else {
    print(min-max+1);
  }
}
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
#define all(v) begin(v), end(v)
#define sz(v) (int)(v).size()
#define fi first
#define se second

const int md = 998244353;

namespace modular {
  inline int mul() { return 1; }
  template <typename... V>
  inline int mul(int a, V... v) {
  #if !defined(_WIN32) || defined(_WIN64)
    return (int) ((long long) a * mul(v...) % md);
  #endif
    unsigned long long x = (long long) a * mul(v...);
    unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m;
    asm(
      "divl %4; \n\t"
      : "=a" (d), "=d" (m)
      : "d" (xh), "a" (xl), "r" (md)
    );
    return m;
  }
  template<typename... V>
  inline void mulby(int &a, V... v) {
    a = mul(a, v...);
  }
  inline int add() { return 0; }
  template<typename... V>
  inline int add(int x, V... v) {
    x += add(v...); if(x >= md) x -= md; return x;
  }
  template<typename... V>
  inline void addto(int &a, V... v) {
    a = add(a, v...);
  }
  inline int sub(int a, int b) {
    if((a -= b) < 0) a += md;
    return a;
  }
  inline void subfrom(int &a, int b) {
    if((a -= b) < 0) a += md;
  }
  inline int qpow(int b, int p) {
    if(p < 0) {
      return qpow(qpow(b, -p), md-2);
    } int ret = 1;
    while(p) {
      if(p & 1) mulby(ret, b);
      mulby(b, b); p >>= 1;
    } return ret;
  }
} using namespace modular;

int main(int argc, char const *argv[])
{
  cin.sync_with_stdio(0); cin.tie(0);
  cin.exceptions(cin.failbit);
#ifdef LOCAL
  freopen("in", "r", stdin);
#endif
  int n, m;
  cin >> n >> m;
  vector<vi> pw(m+1, vi(n+1, 1));
  for(int i = 0; i <= m; ++i) {
    for(int j = 1; j <= n; ++j) {
      pw[i][j] = mul(pw[i][j-1], i);
    }
  }
  vi sum(n, 0);
  for(int i = 0; i < n; ++i) {
    for(int x = 1; x <= m; ++x) {
      addto(sum[i], pw[m-x][i]);
    }
  }
  int ans = 0;
  for(int i = 1; i <= n; ++i) {
    for(int j = i+1; j <= n; ++j) {
      addto(ans, mul(sum[j-i-1], pw[m][n-(j-i)-1]));
    }
  }
  cout << sub(mul(n, pw[m][n]), ans) << endl;
  return 0;
}
/*
* use std::array instead of std::vector, if u can
* overflow?
* array bounds
*/