Problem p03914 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int W = 0; W < (n); W++)                                              \
      cerr << v[W] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(10)

#define MOD 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

ll dp[350][350][350];

int main() {
  int n, m;
  cin >> n >> m;

  dp[0][n - 1][1] = 1;

  REP(i, m) {
    REP(j, n + 1) {
      REP(k, n + 1) {
        if (j > 0) {
          dp[i + 1][j - 1][k] = (dp[i + 1][j - 1][k] + j * dp[i][j][k]) % MOD;
        }

        if (j + k < n) {
          dp[i + 1][j][n - j] = (dp[i + 1][j][n - j] + k * dp[i][j][k]) % MOD;
          dp[i + 1][j][k] = (dp[i + 1][j][k] + (n - j - k) * dp[i][j][k]) % MOD;
        } else {
          dp[i + 1][j][k] = (dp[i + 1][j][k] + k * dp[i][j][k]) % MOD;
        }
      }
    }
    //		SHOW2d(dp[i+1],n+1,n+1);
  }

  cout << dp[m][0][n] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int W = 0; W < (n); W++)                                              \
      cerr << v[W] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(10)

#define MOD 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

ll dp[310][310][310];

int main() {
  int n, m;
  cin >> n >> m;

  dp[0][n - 1][1] = 1;

  REP(i, m) {
    REP(j, n + 1) {
      REP(k, n + 1) {
        if (j > 0) {
          dp[i + 1][j - 1][k] = (dp[i + 1][j - 1][k] + j * dp[i][j][k]) % MOD;
        }

        if (j + k < n) {
          dp[i + 1][j][n - j] = (dp[i + 1][j][n - j] + k * dp[i][j][k]) % MOD;
          dp[i + 1][j][k] = (dp[i + 1][j][k] + (n - j - k) * dp[i][j][k]) % MOD;
        } else {
          dp[i + 1][j][k] = (dp[i + 1][j][k] + k * dp[i][j][k]) % MOD;
        }
      }
    }
    //		SHOW2d(dp[i+1],n+1,n+1);
  }

  cout << dp[m][0][n] << endl;

  return 0;
}