#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef	long long ll;
typedef long double ld;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef set<int> si;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define fi first
#define se second
#define pi 3.141592653589793
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fio ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"
#define sleep for (int i = 1, a;i < 100000000;i++, a = a * a)
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ranked_pairset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_map;

int main() {
	fio;
	int n;
	cin >> n;
	vl a(n + 1);
	for (int i = 1;i <= n;i++)
		cin >> a[i];
	sort(all(a));
	ll ans = 1;
	for (int i = 1;i <= n;i++)
		ans = ans * (a[i] - a[i - 1] + 1) % mod;
	cout << ans << '\n';
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#define N 201000
template<typename T> inline void read(T &x) {
	x = 0; char c = getchar(); bool flag = false;
	while (!isdigit(c)) { if (c == '-') flag = true; c = getchar(); }
	while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
	if (flag)	x = -x;
}
using namespace std;
int n;
int a[N], b[N], h[N], htot;
long long ans;
inline void sol(int L, int R) {
	if (L + 1 == R)	return ;
//	printf("sol(%d, %d) : \n", L, R);
	int ptr = R;
	for (int i = L + 1; i < R; ++i) if (b[i] > a[i]) { ptr = i; break; }
//	printf("ptr = %d\n", ptr);
	int tol = a[ptr - 1];
	for (int i = ptr - 1; i > L; --i) {
		if (b[i] < tol) {
			int p = upper_bound(a + L, a + R + 1, b[i]) - a - 1;//bug
//			printf("i = %d, p = %d\n", i, p);
			ans += i - p;
			tol = b[i];
		}
	}
	int tor = a[ptr];
	for (int i = ptr; i < R; ++i) {
		if (b[i] > tor) {
			int p = lower_bound(a + L, a + R + 1, b[i]) - a;//bug bug
//			printf("i = %d, p = %d\n", i, p);
			ans += p - i;
			tor = b[i];
		}
	}
}
int main() {
	read(n);
	int L;
	read(L);
	++n; ++n;
	a[0] = b[0] = 0; a[n - 1] = b[n - 1] = L + 2 - n;//0 ... L + 1
	for (int i = 1; i < n - 1; ++i) read(a[i]), a[i] -= i;
	for (int i = 1; i < n - 1; ++i) read(b[i]), b[i] -= i;
//	for (int i = 0; i < n; ++i) printf("%d ", a[i]); puts("");
//	for (int i = 0; i < n; ++i) printf("%d ", b[i]); puts("");
	for (int i = 0; i < n; ++i) h[++htot] = a[i];
	htot = unique(h + 1, h + 1 + htot) - h - 1; h[htot + 1] = -12341;
	for (int i = 0; i < n; ++i) a[i] = lower_bound(h + 1, h + 1 + htot, a[i]) - h;
	for (int i = 0; i < n; ++i) {
		int v = lower_bound(h + 1, h + 1 + htot, b[i]) - h;
		if (h[v] != b[i]) return puts("-1"), 0;
		b[i] = v;
	}
//	for (int i = 0; i < n; ++i) printf("%d ", a[i]); puts("");
//	for (int i = 0; i < n; ++i) printf("%d ", b[i]); puts("");
	int lst = 0;
	for (int i = 1; i < n; ++i) if (a[i] == b[i]) {
//		printf("\ni = %d : \n", i);
		sol(lst, i); lst = i;
//		printf("ans = %lld\n", ans);
	}
	printf("%lld\n", ans);
	return 0;
}
