#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using mint = modint; mint comb[1010][1010]; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, p; cin >> n >> p; mint::set_mod(p); comb[0][0] = 1; rep(n, 1009) rep(k, n + 1) comb[n+1][k] += comb[n][k], comb[n+1][k+1] += comb[n][k]; if (n == 1) { cout << 1 << '\n'; return 0; } mint ans = mint(n).pow(n - 2); for (int s = 1; s <= n - 1; s++) { int rest = n - s; int N = rest + 1; mint cs = s == 1 ? mint(1) : mint(s).pow(s - 2); for (int i = 0; i <= N - 2; i++) { ans += comb[N - 2][i] * mint(N - 1).pow(N - 2 - i) * mint(s).pow(i + 1) * cs * comb[n][s]; } } cout << ans.val() << '\n'; }