#include <bits/stdc++.h>
using namespace std;
map<long long, long long> mp;
long long a[55], b[55], m[55];
bool prime[40005];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long extgcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  long long xx, yy;
  long long d = extgcd(b, a % b, xx, yy);
  x = yy, y = xx - (a / b) * yy;
  return d;
}
long long mod_inverse(long long a, long long m) {
  long long x, y;
  if (extgcd(a, m, x, y) != 1) return -1;
  return (x % m + m) % m;
}
pair<long long, long long> congruence(long long a, long long b, long long m) {
  long long d = gcd(a, m);
  if (b % d) return make_pair(0, -1);
  a /= d, b /= d, m /= d;
  return make_pair(b * mod_inverse(a, m) % m, m);
}
pair<long long, long long> SimultaneousCongruence(long long a[], long long b[],
                                                  long long m[], long long n) {
  long long x = 0, M = 1;
  for (int i = 1; i <= n; i++) {
    pair<long long, long long> res =
        congruence(a[i] * M, (b[i] - a[i] * x % m[i] + m[i]) % m[i], m[i]);
    if (res.second == -1) return res;
    x += M * res.first, M *= res.second;
  }
  return make_pair(x, M);
}
const int FACT_MAX = 4000005;
long long q[FACT_MAX], e[FACT_MAX];
long long modpow(long long a, long long n, long long mod) {
  if (n == 0) return 1;
  if (n % 2) {
    return ((a % mod) * (modpow(a, n - 1, mod) % mod)) % mod;
  } else {
    return modpow((a * a) % mod, n / 2, mod) % mod;
  }
}
void make_fact(long long p, long long mod) {
  long long qval = 1, eval = 0;
  q[0] = 1, e[0] = 0;
  for (int i = 1; i < FACT_MAX; i++) {
    long long t = i;
    while (t % p == 0) {
      eval++;
      t /= p;
    }
    qval *= t, qval %= mod;
    q[i] = qval, e[i] = eval;
  }
}
long long comb(long long n, long long k, long long p, long long ex,
               long long mod) {
  long long eval = e[n] - e[k] - e[n - k];
  if (eval >= ex) return 0;
  long long ret = q[n] * mod_inverse(q[k] * q[n - k] % mod, mod) % mod;
  ret *= modpow(p, eval, mod), ret %= mod;
  return ret;
}
long long n, M, l, r;
long long get(long long n, long long r, long long p, long long ex,
              long long mod) {
  if (n % 2 != r % 2) r++;
  if (r > n) return 0;
  long long h = (n - r) / 2;
  long long ret = comb(n, h, p, ex, mod);
  return ret;
}
long long calc(long long p, long long ex, long long mod) {
  make_fact(p, mod);
  long long ret = 0;
  for (long long(i) = (0); (i) <= (n); (i)++) {
    long long tmp =
        get(n - i, l, p, ex, mod) - get(n - i, r + 1, p, ex, mod) + mod;
    tmp *= comb(n, i, p, ex, mod), tmp %= mod;
    ret += tmp, ret %= mod;
  }
  return ret;
}
int main(void) {
  cin >> n >> M >> l >> r;
  for (int i = 2; i < 40005; i++) {
    if (prime[i]) continue;
    for (int j = 2 * i; j < 40005; j += i) prime[j] = true;
  }
  for (int i = 2; i < 40005; i++) {
    if (prime[i]) continue;
    while (M % i == 0) {
      mp[i]++;
      M /= i;
    }
  }
  if (M > 1) mp[M]++;
  long long id = 0;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    id++;
    long long mod = 1;
    for (int i = 0; i < it->second; i++) mod *= it->first;
    a[id] = 1, b[id] = calc(it->first, it->second, mod), m[id] = mod;
  }
  cout << SimultaneousCongruence(a, b, m, id).first << endl;
  return 0;
}
