#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool cmin(T& a, const T& b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool cmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
int inp() {
  int x = 0, f = 1;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x * f;
}
struct comp {
  double r, i;
  comp() {}
  comp(double a, double b) : r(a), i(b) {}
};
const int N = (1 << 19), MaxN = (N << 1) + 10;
const int K = 32767, L = 15;
int P;
comp add(comp a, comp b) { return comp(a.r + b.r, a.i + b.i); }
comp sub(comp a, comp b) { return comp(a.r - b.r, a.i - b.i); }
comp mul(comp a, comp b) {
  return comp(a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r);
}
comp div(comp a, double b) { return comp(a.r / b, a.i / b); }
comp conj(comp x) { return comp(x.r, -x.i); }
int add(int x, int y) { return (x + (long long)y) % P; }
int sub(int x, int y) { return (x - (long long)y + P) % P; }
int mul(int x, int y) { return (long long)x * y % P; }
int exp(int x, long long n) {
  int res = 1;
  for (; n; n >>= 1) {
    if (n & 1) res = mul(res, x);
    x = mul(x, x);
  }
  return res;
}
comp eps[MaxN];
int rv[MaxN];
void init() {
  double pi = acos(-1);
  for (int i = 0; i < N; i++)
    eps[i] = comp(cos(2 * pi * i / N), sin(2 * pi * i / N));
}
inline void trans(int n, comp x[], comp w[]) {
  for (int i = 0, j = 0; i != n; i++) {
    if (i < j) std::swap(x[i], x[j]);
    for (int l = n >> 1; (j ^= l) < l; l >>= 1)
      ;
  }
  for (int i = 2; i <= n; i <<= 1) {
    int l = i >> 1, d = N / i;
    for (int j = 0; j != n; j += i)
      for (int k = 0; k != l; k++) {
        comp t = mul(x[j + k + l], w[d * k]);
        x[j + k + l] = sub(x[j + k], t);
        x[j + k] = add(x[j + k], t);
      }
  }
}
inline void dft(int n, comp x[]) { trans(n, x, eps); }
inline void idft(int n, comp x[]) {
  trans(n, x, eps);
  reverse(x + 1, x + n);
  for (int i = 0; i < n; i++) x[i] = div(x[i], n);
}
void conv(int na, int a[], int nb, int b[], int nc, int c[]) {
  int n, i;
  static comp x[MaxN], y[MaxN], z[MaxN], w[MaxN];
  for (n = 1; n < na + nb - 1; n <<= 1)
    ;
  for (i = 0; i < n; i++) {
    x[i] = i < na ? comp(a[i] & K, a[i] >> L) : comp(0, 0);
    y[i] = i < nb ? comp(b[i] & K, b[i] >> L) : comp(0, 0);
  }
  dft(n, x);
  dft(n, y);
  comp r0(0.5, 0), r1(0, -0.5), r(0, 1);
  for (i = 0; i < n; i++) {
    int j = (n - i) & (n - 1);
    comp x0, x1, y0, y1;
    x0 = mul(add(x[i], conj(x[j])), r0);
    x1 = mul(sub(x[i], conj(x[j])), r1);
    y0 = mul(add(y[i], conj(y[j])), r0);
    y1 = mul(sub(y[i], conj(y[j])), r1);
    z[i] = mul(x0, add(y0, mul(y1, r)));
    w[i] = mul(x1, add(y0, mul(y1, r)));
  }
  idft(n, z);
  idft(n, w);
  for (i = 0; i < nc; i++) {
    int c00 = (long long)(z[i].r + 0.5) % P,
        c01 = (long long)(z[i].i + 0.5) % P;
    int c10 = (long long)(w[i].r + 0.5) % P,
        c11 = (long long)(w[i].i + 0.5) % P;
    c[i] = ((((long long)c11 << L) + c01 + c10 << L) + c00) % P;
  }
}
int nL;
void pow(int x[], int y[], int n) {
  y[0] = 1;
  int sN = 3;
  for (; n; n >>= 1) {
    if (n & 1) conv(sN, x, nL, y, nL, y);
    conv(sN, x, sN, x, sN << 1, x);
    sN <<= 1;
  }
}
int n, l, r, x[MaxN], y[MaxN];
int res(int x) { return y[x + n]; }
int main() {
  int ans;
  init();
  cin >> n >> P >> l >> r;
  nL = 2 * n + 3;
  x[0] = x[1] = x[2] = 1;
  pow(x, y, n);
  if (l == r)
    ans = ((long long)res(r) - res(r + 2) + P) % P;
  else
    ans = ((long long)res(l) + res(l + 1) - res(r + 1) - res(r + 2)) % P;
  cout << ((long long)ans + P) % P << endl;
  return 0;
}
