inlinelongcalculate(constint n){ registerlong ans = 1; for (registerint i = 0; i < n; i++) { for (registerint j = i + 1; j < n; j++) { registerint x = i, y = j; while (a[y][i]) { registerlong t = a[x][i] / a[y][i]; for (registerint k = i; k < n; k++) { a[x][k] = (a[x][k] - a[y][k] * t) % MOD; } std::swap(x, y); } if (x != i) { for (registerint k = 0; k < n; k++) { std::swap(a[i][k], a[x][k]); } sign ^= 1; } } if (a[i][i] == 0) return0; else ans = ans * a[i][i] % MOD; } if (sign) ans = -ans; if (ans < 0) ans += MOD; return ans; }
inlineintmodPow(int a, int b, int mod){ registerint ans = 1; for (; b; b >>= 1, a = (long)a * a % mod) if (b & 1) ans = (long)ans * a % mod; return ans; }
int fac[MAXM * 2 + 5], inv[MAXM * 2 + 5];
inlinevoidinit(constint n){ fac[0] = 1; for (registerint i = 1; i <= n; i++) fac[i] = (long)fac[i - 1] * i % MOD; inv[n - 1] = modPow(fac[n - 1], MOD - 2, MOD); for (registerint i = n - 2; i >= 0; i--) inv[i] = (long)inv[i + 1] * (i + 1) % MOD; }
inlinevoidsolve(){ read(n), read(k); init(n << 1 | 1); staticint x[MAXN], y[MAXN]; for (registerint i = 0; i < k; i++) read(x[i]); for (registerint i = 0; i < k; i++) read(y[i]); for (registerint i = 0; i < k; i++) { for (registerint j = 0; j < k; j++) { a[i][j] = y[j] < x[i] ? 0 : (long)fac[n - 1 + y[j] - x[i]] * inv[n - 1] % MOD * (long)inv[y[j] - x[i]] % MOD; } } print(calculate(k) % MOD);