Problem:
Score : 900 points

Problem Statement

In "Takahashi-ya", a ramen restaurant, basically they have one menu: "ramen", but N kinds of toppings are also offered. When a customer orders a bowl of ramen, for each kind of topping, he/she can choose whether to put it on top of his/her ramen or not. There is no limit on the number of toppings, and it is allowed to have all kinds of toppings or no topping at all. That is, considering the combination of the toppings, 2^N types of ramen can be ordered.

Akaki entered Takahashi-ya. She is thinking of ordering some bowls of ramen that satisfy both of the following two conditions:

Do not order multiple bowls of ramen with the exactly same set of toppings.

Each of the N kinds of toppings is on two or more bowls of ramen ordered.

You are given N and a prime number M. Find the number of the sets of bowls of ramen that satisfy these conditions, disregarding order, modulo M. Since she is in extreme hunger, ordering any number of bowls of ramen is fine.

Constraints

2 \leq N \leq 3000

10^8 \leq M \leq 10^9 + 9

N is an integer.

M is a prime number.

Subscores

600 points will be awarded for passing the test set satisfying N ≤ 50.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of the sets of bowls of ramen that satisfy the conditions, disregarding order, modulo M.

Sample Input 1

2 1000000007

Sample Output 1

2

Let the two kinds of toppings be A and B. Four types of ramen can be ordered: "no toppings", "with A", "with B" and "with A, B". There are two sets of ramen that satisfy the conditions:

The following three ramen: "with A", "with B", "with A, B".

Four ramen, one for each type.

Sample Input 2

3 1000000009

Sample Output 2

118

Let the three kinds of toppings be A, B and C. In addition to the four types of ramen above, four more types of ramen can be ordered, where C is added to the above four. There are 118 sets of ramen that satisfy the conditions, and here are some of them:

The following three ramen: "with A, B", "with A, C", "with B, C".

The following five ramen: "no toppings", "with A", "with A, B", "with B, C", "with A, B, C".

Eight ramen, one for each type.

Note that the set of the following three does not satisfy the condition: "'with A', 'with B', 'with A, B'", because C is not on any of them.

Sample Input 3

50 111111113

Sample Output 3

1456748

Remember to print the number of the sets modulo M. Note that these three sample inputs above are included in the test set for the partial score.

Sample Input 4

3000 123456791

Sample Output 4

16369789

This sample input is not included in the test set for the partial score.

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
fn pow_mod(base: i64, power: i64, modulo: i64) -> i64 {
    let mut base = base;
    let mut power = power;
    let mut ans = 1;
    while power > 0 {
        if power & 1 == 1 {
            ans = ans * base % modulo;
        }
        power >>= 1;
        base = base * base % modulo;
    }
    ans
}

#[allow(dead_code)]
fn inv_mod(a: i64, modulo: i64) -> i64 {
    pow_mod(a, modulo - 2, modulo)
}

const FACT_MAX: usize = 100000;
static mut FACTORIAL: [i64; FACT_MAX + 1] = [0; FACT_MAX + 1];
static mut RFACTORIAL: [i64; FACT_MAX + 1] = [0; FACT_MAX + 1];
fn init_fact(modulo: i64) {
    unsafe {
        if FACTORIAL[0] == 0 {
            FACTORIAL[0] = 1;
            for i in 1..FACT_MAX + 1 {
                FACTORIAL[i] = (FACTORIAL[i - 1] * i as i64) % modulo;
            }
            RFACTORIAL[FACT_MAX] = inv_mod(FACTORIAL[FACT_MAX], modulo);
            for i in (0..FACT_MAX).rev() {
                RFACTORIAL[i] = RFACTORIAL[i + 1] * (i as i64 + 1) % modulo;
            }
        }
    }
}
#[allow(dead_code)]
fn fact(n: i64, modulo: i64) -> i64 {
    let n = n as usize;
    init_fact(modulo);
    unsafe { FACTORIAL[n] }
}
#[allow(dead_code)]
fn rfact(n: i64, modulo: i64) -> i64 {
    let n = n as usize;
    init_fact(modulo);
    unsafe { RFACTORIAL[n] }
}
#[allow(dead_code, unused_comparisons)]
fn combi(n: i64, m: i64, modulo: i64) -> i64 {
    if n < m || m < 0 {
        return 0;
    }
    let n = n as usize;
    let m = m as usize;
    init_fact(modulo);
    unsafe { FACTORIAL[n] * RFACTORIAL[m] % modulo * RFACTORIAL[n - m] % modulo }
}

// fn calc_inner(
//     memo: &mut Vec<Vec<i64>>,
//     modulo: i64,
//     v: usize,
//     rest: usize,
//     cnt: usize,
//     amp: i64,
// ) -> i64 {
//     println!("inner: {} {} {} {}", v, rest, cnt, amp);
//     let mut ret = calc(memo, modulo, v + 1, rest) * amp % modulo * rfact(cnt, modulo) % modulo;
//     println!("{} {} {}", ret, amp, rfact(cnt, modulo));
//     if v < rest {
//         let namp = (amp * combi(rest, v, modulo)) % modulo;
//         ret = (ret + calc_inner(memo, modulo, v, rest - v, cnt + 1, namp)) % modulo;
//     }
//     ret
// }

// fn calc(memo: &mut Vec<Vec<i64>>, modulo: i64, v: usize, rest: usize) -> i64 {
//     if v > rest {
//         return 1;
//     }
//     if memo[v][rest] != -1 {
//         return memo[v][rest];
//     }
//     let ret = calc_inner(memo, modulo, v, rest, 0, 1) % modulo;
//     memo[v][rest] = ret;
//     println!("{} {} {}", v, rest, ret);
//     ret
// }

struct Solver {
    n: i64,
    modulo: i64,
    memo2: Vec<Vec<i64>>,
}

impl Solver {
    fn new() -> Solver {
        let n: i64 = read();
        let modulo: i64 = read();
        Solver {
            n: n,
            modulo: modulo,
            memo2: vec![vec![-1; n as usize + 1]; n as usize + 1],
        }
    }

    fn calc2(&mut self, rest: i64, w: i64) -> i64 {
        if w < rest {
            return 0;
        }
        if rest == 0 {
            return 1;
        }
        if self.memo2[rest as usize][w as usize] != -1 {
            return self.memo2[rest as usize][w as usize];
        }
        let mut ret = self.calc2(rest, w - 1);
        ret = (ret + self.calc2(rest - 1, w - 1)) % self.modulo;
        ret = (ret + self.calc2(rest, w - 1) * rest) % self.modulo;
        self.memo2[rest as usize][w as usize] = ret;
        ret
    }
    fn calc(&mut self, w: i64) -> i64 {
        let modulo = self.modulo;
        let other = self.n - w;
        let mut ret = 0;
        for cnt in 0..w + 1 {
            ret = (ret + self.calc2(cnt, w) * pow_mod(2, other * cnt, modulo) % modulo) % modulo;
        }
        ret
    }
    fn solve(&mut self) -> i64 {
        let n = self.n;
        let modulo = self.modulo;
        let mut sum = 0;
        for i in 0..n + 1 {
            let target = self.calc(i) * combi(n, i, modulo) % modulo;
            let other = pow_mod(2, pow_mod(2, n - i, modulo - 1), modulo);
            let sign = if i % 2 == 0 { 1 } else { -1 };
            sum = (sum + target * other % modulo * sign + modulo) % modulo;
        }
        sum
    }
}

fn main() {
    let mut solver = Solver::new();
    let ans = solver.solve();
    println!("{}", ans);
}