#line 1 "A.cpp" #include using namespace std; using ll = long long; using ull = unsigned long long; template using pq = priority_queue; template using qp = priority_queue, greater>; #define vec(T, A, ...) vector A(__VA_ARGS__); #define vvec(T, A, h, ...) vector> A(h, vector(__VA_ARGS__)); #define vvvec(T, A, h1, h2, ...) vector>> A(h1, vector>(h2, vector(__VA_ARGS__))); #define endl "\n" #define spa ' ' #define len(A) A.size() #define all(A) begin(A), end(A) #define fori1(a) for(ll _ = 0; _ < (a); _++) #define fori2(i, a) for(ll i = 0; i < (a); i++) #define fori3(i, a, b) for(ll i = (a); i < (b); i++) #define fori4(i, a, b, c) for(ll i = (a); ((c) > 0 || i > (b)) && ((c) < 0 || i < (b)); i += (c)) #define overload4(a, b, c, d, e, ...) e #define fori(...) overload4(__VA_ARGS__, fori4, fori3, fori2, fori1)(__VA_ARGS__) #define INT(...) int __VA_ARGS__; inp(__VA_ARGS__); #define LL(...) ll __VA_ARGS__; inp(__VA_ARGS__); #define STRING(...) string __VA_ARGS__; inp(__VA_ARGS__); #define CHAR(...) char __VA_ARGS__; inp(__VA_ARGS__); #define VEC(T, A, n) vector A(n); inp(A); #define VVEC(T, A, n, m) vector> A(n, vector(m)); inp(A); const ll MOD1 = 1000000007; const ll MOD9 = 998244353; template auto min(const T& a){ return *min_element(all(a)); } template auto max(const T& a){ return *max_element(all(a)); } template inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } void print(){cout << endl;} template void print(Head &&head, Tail &&... tail) { cout << head; if (sizeof...(Tail)) cout << spa; print(forward(tail)...); } template void print(vector &A){ int n = A.size(); for(int i = 0; i < n; i++){ cout << A[i]; if(i == n - 1) cout << endl; else cout << spa; } } template void print(vector> &A){ for(auto &row: A) print(row); } template void print(pair &A){ cout << A.first << spa << A.second << endl; } template void print(vector> &A){ for(auto &row: A) print(row); } template void prisep(vector &A, S sep){ int n = A.size(); for(int i = 0; i < n; i++){ cout << A[i]; if(i == n - 1) cout << endl; else cout << sep; } } template void priend(T A, S end){ cout << A << end; } template void priend(T A){ priend(A, spa); } template void inp(T&... a){ (cin >> ... >> a); } template void inp(vector &A){ for(auto &a:A) cin >> a; } template void inp(vector> &A){ for(auto &row:A) inp(row); } template void inp(pair &A){ inp(A.first, A.second); } template void inp(vector> &A){ for(auto &row: A) inp(row.first, row.second); } template T sum(vector &A){ T tot = 0; for(auto a:A) tot += a; return tot; } #line 2 "Library/C++/math/pollard_rho.hpp" #line 2 "Library/C++/math/modpow.hpp" template T modpow(T a, long long b, T MOD){ T ret = 1; while(b > 0){ if(b & 1){ ret *= a; ret %= MOD; } a *= a; a %= MOD; b >>= 1; } return ret; } #line 3 "Library/C++/math/millerRabin.hpp" bool isPrime(long long n){ if(n <= 1) return false; else if(n == 2) return true; else if(n % 2 == 0) return false; long long A[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; long long s = 0; long long d = n - 1; while(d % 2 == 0){ d /= 2; s++; } for(auto a: A){ if(a % n == 0) return true; long long x = modpow<__int128_t>(a, d, n); if(x != 1){ bool ng = true; for(int i = 0; i < s; i++){ if(x == n - 1){ ng = false; break; }; x = __int128_t(x) * x % n; } if(ng) return false; } } return true; } #line 4 "Library/C++/math/pollard_rho.hpp" long long pollard(long long N) { if (N % 2 == 0) return 2; if (isPrime(N)) return N; auto f = [&](long long x) -> long long { return (__int128_t(x) * x + 1) % N; }; long long step = 0; while (true) { ++step; long long x = step, y = f(x); while (true) { long long p = gcd(y - x + N, N); if (p == 0 || p == N) break; if (p != 1) return p; x = f(x); y = f(f(y)); } } } vector primefact(long long N) { if (N == 1) return {}; long long p = pollard(N); if (p == N) return {p}; vector left = primefact(p); vector right = primefact(N / p); left.insert(left.end(), right.begin(), right.end()); sort(left.begin(), left.end()); return left; } #line 2 "Library/C++/math/modinv.hpp" template T modinv(T a, T MOD){ T b = MOD; T u = 1; T v = 0; while(b > 0){ T t = a / b; a -= t * b; u -= t * v; swap(a, b); swap(u, v); } if(a != 1) return -1; if(u < 0) u += MOD; return u; } #line 2 "Library/C++/math/ext_gcd.hpp" template vector ext_gcd(T a, T b){ // return (x, y, gcd(a, b)) s.t. ax + by = gcd(a, b) if(a == 0) return {0, 1, b}; else{ auto tmp = ext_gcd(b % a, a); T x = tmp[0]; T y = tmp[1]; T g = tmp[2]; return {y - b / a * x, x, g}; } } #line 3 "Library/C++/math/Garner.hpp" pair Garner(vector &R, vector &M){ int n = R.size(); long long r = 0; long long m = 1; for(int i = 0; i < n; i++){ long long ri = R[i]; long long mi = M[i]; if(ri < 0 || mi <= ri){ ri = (ri % mi + mi) % mi; } if(m < mi){ swap(m, mi); swap(r, ri); } if(m % mi == 0){ if(r % mi != ri) return {0, 0}; continue; } long long g, im; auto res = ext_gcd(m, mi); g = res[2]; im = res[0]; // print(m, mi, im, g); if(im < 0) im += mi; long long ui = mi / g; if((ri - r) % g != 0) return {0, 0}; long long x = (ri - r) / g % ui * im % ui; r += x * m; m *= ui; if (r < 0) r += m; } return {r, m}; } #line 6 "Library/C++/math/arbitrary_mod_nCk.hpp" struct prime_power_mod_nCk{ int p, e, m; vector fact, invfact; prime_power_mod_nCk(int p, int e): p(p), e(e){ m = 1; for(int i = 0; i < e; i++) m *= p; fact.resize(m + 1); invfact.resize(m + 1); fact[0] = 1; invfact[0] = 1; for(long long i = 1; i <= m; i++){ if(i % p == 0) fact[i] = fact[i - 1]; else fact[i] = fact[i - 1] * i % m; invfact[i] = modinv(fact[i], m); } } long long C(long long n, long long k){ if(n < 0 || n < k || k < 0) return 0; long long ret = 1; long long r = n - k; int e0 = 0, eq = 0, i = 0; while(n > 0){ ret = ret * fact[n % m] % m; ret = ret * invfact[k % m] % m; ret = ret * invfact[r % m] % m; n /= p; k /= p; r /= p; e0 += n - k - r; if(e0 >= e) return 0; i++; if(i >= e) eq += n - k - r; } if(!(p == 2 && e >= 3) && (eq & 1)){ ret = ret * (m - 1) % m; } ret *= modpow(p, e0, m); return ret % m; } }; struct arbitrary_mod_nCk{ int MOD; vector M; vector prime_nCk; arbitrary_mod_nCk(int MOD) : MOD(MOD){ if(MOD == 1) return; auto primes = primefact(MOD); int row = 0; int bef = primes[0]; primes.push_back(-1); for(auto p:primes){ if(p == bef) row++; else{ int x = 1; for(int i = 0; i < row; i++){ x *= bef; } M.push_back(x); prime_nCk.push_back(prime_power_mod_nCk(bef, row)); bef = p; row = 1; } } } long long nCk(long long n, long long k){ if(MOD == 1) return 0; vector R(M.size()); for(int i = 0; i < M.size(); i++){ R[i] = prime_nCk[i].C(n, k); } return Garner(R, M).first; } }; #line 125 "A.cpp" void solve(){ LL(L, R, M); arbitrary_mod_nCk C(M); ll ans = 0; fori(i, L, R + 1){ ans += C.nCk(2 * i, i) - 2; ans %= M; } if(ans < 0) ans += M; print(ans); } int main(){ cin.tie(0)->sync_with_stdio(0); int t; t = 1; // cin >> t; while(t--) solve(); return 0; }