#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
map<int, int> M;
vector<pair<int, int> > V;
long long F1[12][100010], F2[12][100010];
int p;
int pw(int x, int y, int M) {
  int res = 1;
  while (y) {
    if (y & 1) res = (long long)res * x % M;
    x = (long long)x * x % M;
    y >>= 1;
  }
  return res;
}
long long gc(long long x, long long y) { return y == 0 ? x : gc(y, x % y); }
pair<long long, long long> helper(long long a, long long b) {
  if (!b)
    return pair<long long, long long>(1, 0);
  else {
    pair<long long, long long> c = helper(b, a % b);
    return pair<long long, long long>(c.second, c.first - a / b * c.second);
  }
}
long long my_mul2(long long a, long long b, long long m) { return a * b % m; }
long long DODO(vector<pair<long long, long long> > &L) {
  if (((int)(L).size()) == 0) return 0;
  if (L.size() == 1)
    return L[0].first;
  else {
    pair<long long, long long> a = L.back();
    L.pop_back();
    pair<long long, long long> b = L.back();
    L.pop_back();
    pair<long long, long long> t = helper(a.second, b.second);
    long long g = gc(a.second, b.second);
    if ((b.first - a.first) % g != 0) return -1;
    long long m = b.second / g * a.second;
    long long x =
        (my_mul2((b.first - a.first) / g % m + m, (t.first * a.second % m), m) +
         a.first) %
        m;
    x = (x % m + m) % m;
    L.emplace_back(x, m);
    return DODO(L);
  }
}
int Get(int n, int r) {
  if (r > n) return 0;
  vector<pair<long long, long long> > L;
  for (int i = 0; i < ((int)(V).size()); i++) {
    int p = V[i].first;
    long long px = 1;
    for (int b = 0; b < V[i].second; b++) px = px * p;
    int val = (int)(F1[i][n] - F1[i][r] - F1[i][n - r]);
    long long g = 0;
    if (val >= V[i].second)
      g = 0;
    else {
      int ppx = 1, ppy = 1;
      for (int b = 0; b < V[i].second - val; b++) ppx = ppx * p;
      ppy = ppx / p * (p - 1);
      long long temp = F2[i][n] * pw((int)F2[i][r], ppy - 1, ppx) % ppx *
                       pw((int)F2[i][n - r], ppy - 1, ppx) % ppx;
      g = (int)temp;
      for (int b = 0; b < val; b++) g = g * p;
    }
    L.push_back(pair<long long, long long>(g, px));
  }
  return (int)DODO(L);
}
int Do(int n, int x) {
  if (x < 0) return 0;
  long long res = 0;
  for (int j = 0; j <= n; j++) {
    long long t = Get(n, j);
    long long t2 = Get(n + 1 - j, 1 + j);
    if (n - j + 1 >= x + j + 2) t2 = t2 - Get(n + 1 - j, x + j + 2);
    if (t2 < 0) t2 += p;
    t = t * t2 % p;
    res = (res + t) % p;
  }
  return (int)res;
}
void solve() {
  int n, l, r;
  scanf("%d%d%d%d", &n, &p, &l, &r);
  int tp = p;
  for (int i = 2; (long long)i * i <= tp; i++) {
    while (tp % i == 0) {
      M[i]++;
      tp /= i;
    }
  }
  if (tp > 1) M[tp]++;
  for (pair<int, int> e : M) V.push_back(e);
  int m = ((int)(V).size());
  for (int i = 0; i < m; i++) {
    F1[i][0] = 0;
    F2[i][0] = 1;
    int p = V[i].first;
    int px = 1;
    for (int b = 0; b < V[i].second; b++) px = px * p;
    for (int a = 1; a < 100010; a++) {
      int cnt = 1, ta = a;
      while (ta % p == 0) ++cnt, ta /= p;
      F1[i][a] = F1[i][a - 1] + cnt;
      F2[i][a] = (long long)F2[i][a - 1] * ta % px;
    }
  }
  int ans = Do(n, r) - Do(n, l - 1);
  if (ans < 0) ans += p;
  printf("%d\n", ans);
}
int main() {
  int Tc = 1;
  for (int tc = 1; tc <= Tc; tc++) {
    solve();
  }
  return 0;
}
