#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }


const int MAX = 21000;
int MOD;

long long fac[MAX], finv[MAX], inv[MAX];
void COMinit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < MAX; i++){
        fac[i] = fac[i-1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
        finv[i] = finv[i-1] * inv[i] % MOD;
    }
}
long long COM(int n, int k){
    if(n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD;
}

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

long long modinv(long long a, long long mod) {
    long long b = mod, u = 1, v = 0;
    while (b) {
        long long t = a/b;
        a -= t*b; swap(a, b);
        u -= t*v; swap(u, v);
    }
    u %= mod;
    if (u < 0) u += mod;
    return u;
}


long long func(long long b, long long w) {
    long long res = 0;
    for (long long i = 0; i <= b; ++i) {
        /*
        for (long long j = 0; j <= w; ++j) {
            long long tmp = COM(b, i) * COM(w, j) % MOD * modpow(2LL, i*j, MOD) % MOD;

            cout << b << ", " << w << "; " << i << ", " << j << "; " << tmp << endl;
            if ((b+w+i+j) % 2 == 0) res += tmp;
            else res = (res - tmp + MOD) % MOD;
        }
        */
        
        long long tmp = COM(b, i) * modpow( (1 - modpow(2LL, i, MOD) + MOD) % MOD, w, MOD) % MOD;
        if ((b + w + i) % 2 == 0) res += tmp;
        else res = (res - tmp + MOD) % MOD;
        
    }

    //cout << b << ", " << w << ": " << res << endl;
    return res;
}
                           

int main() {
    long long N;
    while (cin >> N >> MOD) {
        COMinit();
        long long res = 0;
        for (long long b = 1; b <= N-1; ++b) {
            long long w = N-b;
            long long tmp = COM(N, b) * modpow(2LL, b*(b-1)/2 + w*(w-1)/2, MOD) % MOD * func(b, w) % MOD;
            res = (res + tmp) % MOD;

            
        }
        cout << res << endl;
    }
}
    















Measured memory_bytes: 3596