import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class mp3_sm_wrong {

	private void solve() {
		int n = in.nextInt();
		assert 2 <= n && n <= 5000;
		int m = in.nextInt();
		assert 2 <= m && m <= 1e9;
		long[] prev = new long[n + 1];
		long[] next = new long[n + 1];
		prev[0] = prev[1] = 1;
		for (int i = 2; i < n; i++) {
			for (int j = 0; j < i; j++) {
				if (j > 0) {
					next[j - 1] += j * prev[j];
				}
				next[j] += prev[j] * (i - j);
				next[j + 1] += prev[j];
			}
			for (int j = 0; j < i + 1; j++) {
				next[j] %= m;
			}
			long[] a = prev;
			prev = next;
			next = a;
		}
		out.println(prev[0]);
	}

	Scanner in;
	PrintWriter out;

	void run() {
		try {
			in = new Scanner(new File("mp3.in"));
			out = new PrintWriter("mp3.out");
			solve();
			out.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) {
		new mp3_sm_wrong().run();
	}

}
