#include <bits/stdc++.h>
using namespace std;
long long int n, p, l, r, K;
long long int gt[100005], rev[100005], gt1[100005];
long long int mu(long long int a, long long int n, long long int K) {
  if (n == 0) return 1;
  long long int q = mu(a, n / 2, K);
  if (n % 2 == 0) return q * q % K;
  return q * q % K * a % K;
}
long long int Combi(long long int n, long long int k, long long int p) {
  if (n < k || k < 0) return 0;
  long long int u = gt1[n] - gt1[n - k] - gt1[k];
  long long int res = gt[n] * rev[k] % K * rev[n - k] % K;
  res = res * mu(p, u, K) % K;
  return res;
}
long long int solve(long long int p, long long int k) {
  K = 1;
  long long int phi = 1;
  for (int i = 1; i <= k; i++) K *= p;
  phi = K / p * (p - 1);
  gt[0] = rev[0] = 1;
  for (int i = 1; i <= 100000; i++) {
    gt1[i] = gt1[i - 1];
    int j = i;
    while (j % p == 0) {
      j /= p;
      gt1[i]++;
    }
    gt[i] = gt[i - 1] * 1ll * j % K;
    rev[i] = mu(gt[i], phi - 1, K);
  }
  long long int res = 0;
  for (int m = 1; m <= n; m++) {
    int u = (m + l - 1) / 2 + 1;
    int v = (m + r) / 2;
    res =
        (res + Combi(n, m, p) * (Combi(m, u, p) - Combi(m, v + 1, p)) % K + K) %
        K;
  }
  return res;
}
vector<pair<long long int, long long int> > divi;
long long int extended_euclid(long long int a, long long int b,
                              long long int &x, long long int &y) {
  long long int xx = y = 0;
  long long int yy = x = 1;
  while (b) {
    long long int q = a / b;
    long long int t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
long long int CRT(long long int m1, long long int r1, long long int m2,
                  long long int r2) {
  long long int s, t;
  extended_euclid(r1, r2, s, t);
  long long int K = r1 * r2;
  long long int res = (m2 - m1) * s % K * r1 % K;
  res = (res + m1 + K) % K;
  return res;
}
long long int val[100005];
long long int mod[100005];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> p >> l >> r;
  if (p == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 2; i <= p / i; i++) {
    if (p % i == 0) {
      int cnt = 0;
      while (p % i == 0) {
        p /= i;
        cnt++;
      }
      divi.push_back(pair<long long int, long long int>(i, cnt));
    }
  }
  if (p > 1) divi.push_back(pair<long long int, long long int>(p, 1));
  for (int i = 0; i < divi.size(); i++) {
    val[i] = solve(divi[i].first, divi[i].second);
    mod[i] = 1;
    for (int j = 1; j <= divi[i].second; j++) mod[i] *= divi[i].first;
  }
  long long int res = val[0];
  long long int M = mod[0];
  for (int i = 1; i < divi.size(); i++) {
    res = CRT(res, M, val[i], mod[i]);
    M *= mod[i];
  }
  if (l == 0) res = (res + 1) % M;
  cout << res << endl;
  return 0;
}
