#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, p, L, R;
int fact[N], ifact[N], expo[N], prime, power;
int expomod(long long int a, long long int e) {
  int ans = 1;
  while (e) {
    if (e & 1) ans = ans * a % power;
    e /= 2;
    a = a * a % power;
  }
  return ans;
}
void chinese(int &r1, int &m1, int r2, int m2) {
  if (m1 < m2) {
    swap(m1, m2);
    swap(r1, r2);
  }
  while (r1 % m2 != r2) r1 += m1;
  m1 *= m2;
}
int comb(int m, int n) {
  if (n < 0 || n > m) return 0;
  int ans = (long long int)fact[m] * ifact[m - n] % power * ifact[n] % power;
  int k = expo[m] - expo[m - n] - expo[n];
  ans = (long long int)ans * expomod(prime, k) % power;
  return ans;
}
int calc(int n, int bal) {
  long long int ans = 0;
  for (int x = 0, y = bal; x + y <= n; x++, y++) {
    ans += (long long int)comb(n, x) * comb(n - x, y) % power;
  }
  ans %= power;
  if (ans < 0) ans += power;
  return ans;
}
void go(int &r, int &m, int p, int pk) {
  int phi = pk - pk / p;
  prime = p;
  power = pk;
  fact[0] = ifact[0] = 1;
  expo[0] = 0;
  for (int i = (int)(1); i < ((int)n + 1); i++) {
    if (i % p == 0) {
      expo[i] = 1 + expo[i / p];
      fact[i] = fact[i / p];
    } else {
      expo[i] = 0;
      fact[i] = i;
    }
  }
  for (int i = (int)(1); i < ((int)n + 1); i++) {
    expo[i] += expo[i - 1];
    fact[i] = (long long int)fact[i] * fact[i - 1] % pk;
    ifact[i] = expomod(fact[i], phi - 1);
  }
  long long int ans = 0;
  for (int i = 0; i < (int)(2); i++) {
    ans += calc(n, L + i);
    ans -= calc(n, R + 1 + i);
  }
  ans %= pk;
  if (ans < 0) ans += pk;
  chinese(r, m, ans, pk);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> p >> L >> R;
  int ans = 0, mod = 1;
  for (int d = 2; d * d <= p; d++) {
    int dk = 1;
    while (p % d == 0) {
      dk *= d;
      p /= d;
    }
    if (dk > 1) go(ans, mod, d, dk);
  }
  if (p > 1) go(ans, mod, p, p);
  cout << ans << endl;
  return 0;
}
