#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(c) ((c).begin()), ((c).end())
#define sz(x) ((int)(x).size())
#ifdef LOCAL
#include <print.h>
#else
#define trace(...)
#define endl "\n" // remove in interactive
#endif
const int N = 33;
int power(int x, int a, int p){
int ret = 1;
for(; a; a >>= 1, x = (x * (ll) x) % p) if(a & 1) ret = (ret * (ll) x) % p;
return ret;
}
pair<ll, int> dp[2][1400000];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); // Remove in interactive problems
int n, p;
cin >> n;
cin >> p;
vector<int> fact(N * N, 0), invfact(N * N, 0);
fact[0] = 1;
for(int i = 1; i < N * N; i++){
fact[i] = i * (ll) fact[i - 1] % p;
}
invfact[N*N - 1] = power(fact[N*N - 1], p - 2, p);
for(int i = N*N - 2; i >= 0; i--){
invfact[i] = (i + 1) * (ll) invfact[i + 1] % p;
}
assert(invfact[0] == 1);
vector<int> s(n), t(n);
ll MASK = 0;
int delta = 0;
for(int i = 0; i < n; i++){
cin >> s[i];
s[i]--;
delta-=s[i];
}
for(int i = 0; i < n; i++){
cin >> t[i];
t[i]--;
delta += t[i];
}
if(delta<0){
cout << 0 << endl;
return 0;
}
dp[0][0] = {(1LL << n) - 1, fact[delta]};
sort(all(s));
sort(all(t));
int I[2];
I[0] = 1;
function<void(ll, int, int, int)> appendMasks = [&](ll mask, int i, int d, int id){
if(d > i + 1) return;
if(d == 0){
dp[id][I[id]++] = {(1LL << n) - 1 - mask, 0};
return;
}
if(s[i] <= t[d - 1]) appendMasks(mask | (1LL << i), i - 1, d - 1, id);
appendMasks(mask, i - 1, d, id);
};
for(int pos = 0, id = 0; pos < n; pos++, id ^= 1){
int x = t[pos];
I[id ^ 1] = 0;
int maxi = 0;
while(maxi < n && s[maxi] <= x) maxi++; maxi--;
appendMasks(0LL, maxi, pos + 1, id ^ 1);
for(int u = 0; u <= maxi; u++){
int ptr = 0;
for(int i = 0; i < I[id]; i++){
ll mask = dp[id][i].first;
int value = dp[id][i].second;
ll newmask = mask & (~(1LL << u));
if(newmask == mask) continue;
while(ptr < I[id ^ 1] && dp[id ^ 1][ptr].first < newmask) ptr++;
if(dp[id ^ 1][ptr].first == newmask) dp[id ^ 1][ptr].second = (dp[id ^ 1][ptr].second + value * (ll) invfact[x - s[u]]) % p;
}
}
}
cout << dp[n & 1][0].second << endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIGFsbChjKSAoKGMpLmJlZ2luKCkpLCAoKGMpLmVuZCgpKQojZGVmaW5lIHN6KHgpICgoaW50KSh4KS5zaXplKCkpCgojaWZkZWYgTE9DQUwKI2luY2x1ZGUgPHByaW50Lmg+CiNlbHNlCiNkZWZpbmUgdHJhY2UoLi4uKQojZGVmaW5lIGVuZGwgIlxuIiAvLyByZW1vdmUgaW4gaW50ZXJhY3RpdmUKI2VuZGlmCgpjb25zdCBpbnQgTiA9IDMzOwoKaW50IHBvd2VyKGludCB4LCBpbnQgYSwgaW50IHApewogICAgaW50IHJldCA9IDE7CiAgICBmb3IoOyBhOyBhID4+PSAxLCB4ID0gKHggKiAobGwpIHgpICUgcCkgaWYoYSAmIDEpIHJldCA9IChyZXQgKiAobGwpIHgpICUgcDsKICAgIHJldHVybiByZXQ7Cn0KcGFpcjxsbCwgaW50PiBkcFsyXVsxNDAwMDAwXTsKaW50IG1haW4oKXsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOyAKICAgIGNpbi50aWUoTlVMTCk7IC8vIFJlbW92ZSBpbiBpbnRlcmFjdGl2ZSBwcm9ibGVtcwogICAgaW50IG4sIHA7CiAgICBjaW4gPj4gbjsKICAgIGNpbiA+PiBwOwogICAgCiAgICB2ZWN0b3I8aW50PiBmYWN0KE4gKiBOLCAwKSwgaW52ZmFjdChOICogTiwgMCk7CiAgICBmYWN0WzBdID0gMTsKICAgIGZvcihpbnQgaSA9IDE7IGkgPCBOICogTjsgaSsrKXsKICAgICAgICBmYWN0W2ldID0gaSAqIChsbCkgZmFjdFtpIC0gMV0gJSBwOwogICAgfQogICAgaW52ZmFjdFtOKk4gLSAxXSA9IHBvd2VyKGZhY3RbTipOIC0gMV0sIHAgLSAyLCBwKTsKICAgIGZvcihpbnQgaSA9IE4qTiAtIDI7IGkgPj0gMDsgaS0tKXsKICAgICAgICBpbnZmYWN0W2ldID0gKGkgKyAxKSAqIChsbCkgaW52ZmFjdFtpICsgMV0gJSBwOwogICAgfQogICAgYXNzZXJ0KGludmZhY3RbMF0gPT0gMSk7CiAgICB2ZWN0b3I8aW50PiBzKG4pLCB0KG4pOwogICAgbGwgTUFTSyA9IDA7CiAgICBpbnQgZGVsdGEgPSAwOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKyl7CiAgICAgICAgY2luID4+IHNbaV07IAogICAgICAgIHNbaV0tLTsKICAgICAgICBkZWx0YS09c1tpXTsKICAgIH0KICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspewogICAgICAgIGNpbiA+PiB0W2ldOyAKICAgICAgICB0W2ldLS07CiAgICAgICAgZGVsdGEgKz0gdFtpXTsKICAgIH0KICAgIGlmKGRlbHRhPDApewogICAgICAgIGNvdXQgPDwgMCA8PCBlbmRsOwogICAgICAgIHJldHVybiAwOwogICAgfQogICAgZHBbMF1bMF0gPSB7KDFMTCA8PCBuKSAtIDEsIGZhY3RbZGVsdGFdfTsKICAgIHNvcnQoYWxsKHMpKTsKICAgIHNvcnQoYWxsKHQpKTsKICAgIGludCBJWzJdOwogICAgSVswXSA9IDE7CiAgICBmdW5jdGlvbjx2b2lkKGxsLCBpbnQsIGludCwgaW50KT4gYXBwZW5kTWFza3MgPSBbJl0obGwgbWFzaywgaW50IGksIGludCBkLCBpbnQgaWQpewogICAgICAgIGlmKGQgPiBpICsgMSkgcmV0dXJuOwogICAgICAgIGlmKGQgPT0gMCl7CiAgICAgICAgICAg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