This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/binomial_coefficient_prime_mod"
#include <bits/stdc++.h>
using namespace std;
#include "../../../lib/math/arbitrary_modint.hpp"
#include "../../../lib/math/combination.hpp"
using mint = ModInt;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t, m; cin >> t >> m;
mint::setMod(m);
Combination comb(10000000, m);
while(t--){
int n, k; cin >> n >> k;
mint ans = comb.ncr(n, k);
cout << ans << "\n";
}
}
#line 1 "test/library_checker/enumerative_combinatorics/binomial_coefficient_prime_mod_1.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/binomial_coefficient_prime_mod"
#include <bits/stdc++.h>
using namespace std;
#line 2 "lib/math/arbitrary_modint.hpp"
/**
* @brief Arbitrary Modint
* @docs docs/math/arbitrary_modint.md
*/
struct ModInt{
long long val;
ModInt(const long long &_val = 0) noexcept : val(_val) {
normalize();
}
static long long &Modulus(){
static long long mod = 0;
return mod;
}
static long long mod() { return Modulus(); }
static void setMod(const int &mod){
Modulus() = mod;
}
void normalize(){
val = (val % Modulus() + Modulus()) % Modulus();
}
inline ModInt &operator+=(const ModInt &rhs) noexcept {
if(val += rhs.val, val >= Modulus()) val -= Modulus();
return *this;
}
inline ModInt &operator-=(const ModInt &rhs) noexcept {
if(val -= rhs.val, val < 0) val += Modulus();
return *this;
}
inline ModInt &operator*=(const ModInt &rhs) noexcept {
val = val * rhs.val % Modulus();
return *this;
}
inline ModInt &operator/=(const ModInt &rhs) noexcept {
val = val * inv(rhs.val).val % Modulus();
return *this;
}
inline ModInt &operator++() noexcept {
if(++val >= Modulus()) val -= Modulus();
return *this;
}
inline ModInt operator++(int) noexcept {
ModInt t = val;
if(++val >= Modulus()) val -= Modulus();
return t;
}
inline ModInt &operator--() noexcept {
if(--val < 0) val += Modulus();
return *this;
}
inline ModInt operator--(int) noexcept {
ModInt t = val;
if(--val < 0) val += Modulus();
return t;
}
inline ModInt operator-() const noexcept { return (Modulus() - val) % Modulus(); }
inline ModInt inv(void) const { return inv(val); }
ModInt pow(long long n) const {
assert(0 <= n);
ModInt x = *this, r = 1;
while(n){
if(n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
ModInt inv(const long long n) const {
long long a = n, b = Modulus(), u = 1, v = 0;
while(b){
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= Modulus();
if(u < 0) u += Modulus();
return u;
}
friend inline ModInt operator+(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) += rhs; }
friend inline ModInt operator-(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) -= rhs; }
friend inline ModInt operator*(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) *= rhs; }
friend inline ModInt operator/(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) /= rhs; }
friend inline bool operator==(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val == rhs.val; }
friend inline bool operator!=(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val != rhs.val; }
friend inline istream &operator>>(istream &is, ModInt &x) noexcept {
is >> x.val;
x.normalize();
return is;
}
friend inline ostream &operator<<(ostream &os, const ModInt &x) noexcept { return os << x.val; }
};
#line 2 "lib/math/combination.hpp"
/**
* @brief Combination (二項係数)
* @docs docs/math/combination.md
*/
struct Combination{
vector<long long> memo, memoinv, inv;
const long long mod;
Combination(const int &N, const long long &m) : memo(N + 1), memoinv(N + 1), inv(N + 1), mod(m){
memo[0] = memo[1] = 1;
memoinv[0] = memoinv[1] = 1;
inv[1] = 1;
for(int i = 2; i <= N; ++i){
memo[i] = memo[i - 1] * i % mod;
inv[i] = mod - inv[mod % i] * (m / i) % mod;
memoinv[i] = memoinv[i - 1] * inv[i] % mod;
}
}
inline long long fact(const long long &n) const {
return memo[n];
}
inline long long factinv(const long long &n) const {
return memoinv[n];
}
inline long long ncr(const long long &n, const long long &r) const {
if(n < r || r < 0) return 0;
return (memo[n] * memoinv[r] % mod) * memoinv[n - r] % mod;
}
inline long long npr(const long long &n, const long long &r) const {
if(n < r || r < 0) return 0;
return (memo[n] % mod) * memoinv[n - r] % mod;
}
inline long long nhr(const long long &n, const long long &r) const {
if(n == 0 && r == 0) return 1;
return ncr(n + r - 1, r);
}
};
struct CombinationByPascal{
vector<vector<long long>> memo;
const long long mod;
CombinationByPascal(const int &N, const long long &m) : mod(m){
memo.assign(N + 1, vector<long long>(N + 1));
memo[0][0] = 1;
for(int i = 1; i <= N; ++i){
memo[i][0] = 1;
for(int j = 1; j <= N; ++j){
memo[i][j] = (memo[i - 1][j - 1] + memo[i - 1][j]);
if(memo[i][j] >= mod) memo[i][j] -= mod;
}
}
}
inline long long ncr(const int &n, const int &r) const {
if(n < r || r < 0) return 0;
return memo[n][r];
}
};
#line 7 "test/library_checker/enumerative_combinatorics/binomial_coefficient_prime_mod_1.test.cpp"
using mint = ModInt;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t, m; cin >> t >> m;
mint::setMod(m);
Combination comb(10000000, m);
while(t--){
int n, k; cin >> n >> k;
mint ans = comb.ncr(n, k);
cout << ans << "\n";
}
}