Problem p03914 (C++)

Buggy submission (status: Memory Limit Exceeded):
/*

This Submission is to determine how many 120/240 min const. delivery point there
are.

//info
120 req. steps <= 5

test_case 1,6 == 50 delivery point
                 2,3,4,5,7 == 80 delivery point
                                8,9 == 90 delivery point
                                10 > 100 delivery point
*/
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 1000000007
#define seg_size 262144
#define REP(a, b) for (long long a = 0; a < b; ++a)

unsigned long xor128() {
  static unsigned long x = time(NULL), y = 362436069, z = 521288629,
                       w = 88675123;
  unsigned long t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
long long dp[401][401][401] = {}; // day,undetermined,determined
int main() {
  long long n, m;
  cin >> n >> m;
  dp[0][1][1] = 1;
  for (long long i = 0; i < m; ++i) {
    for (long long q = 1; q <= n; ++q) {
      for (long long j = 1; j <= n; ++j) {
        dp[i + 1][q + 1][j] += dp[i][q][j] * (n - q);
        dp[i + 1][q + 1][j] %= MAX_MOD;
        dp[i + 1][q][j] += dp[i][q][j] * (q - j);
        dp[i + 1][q][j] %= MAX_MOD;
        dp[i + 1][q][q] += dp[i][q][j] * j;
        dp[i + 1][q][q] %= MAX_MOD;
      }
    }
  }
  cout << dp[m][n][n] << endl;
}

Fixed submission:
/*

This Submission is to determine how many 120/240 min const. delivery point there
are.

//info
120 req. steps <= 5

test_case 1,6 == 50 delivery point
                 2,3,4,5,7 == 80 delivery point
                                8,9 == 90 delivery point
                                10 > 100 delivery point
*/
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 1000000007
#define seg_size 262144
#define REP(a, b) for (long long a = 0; a < b; ++a)

unsigned long xor128() {
  static unsigned long x = time(NULL), y = 362436069, z = 521288629,
                       w = 88675123;
  unsigned long t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
long long dp[302][302][302] = {}; // day,undetermined,determined
int main() {
  long long n, m;
  cin >> n >> m;
  dp[0][1][1] = 1;
  for (long long i = 0; i < m; ++i) {
    for (long long q = 1; q <= n; ++q) {
      for (long long j = 1; j <= n; ++j) {
        dp[i + 1][q + 1][j] += dp[i][q][j] * (n - q);
        dp[i + 1][q + 1][j] %= MAX_MOD;
        dp[i + 1][q][j] += dp[i][q][j] * (q - j);
        dp[i + 1][q][j] %= MAX_MOD;
        dp[i + 1][q][q] += dp[i][q][j] * j;
        dp[i + 1][q][q] %= MAX_MOD;
      }
    }
  }
  cout << dp[m][n][n] << endl;
}