Problem p03914 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>

typedef long long signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for (int i = (l); i < (r); i++)
#define incII(i, l, r) for (int i = (l); i <= (r); i++)
#define decID(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define decII(i, l, r) for (int i = (r); i >= (l); i--)
#define inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) < (r))

template <typename T> void swap(T &x, T &y) {
  T t = x;
  x = y;
  y = t;
  return;
}
template <typename T> T abs(T x) { return (0 <= x ? x : -x); }
template <typename T> T max(T a, T b) { return (b <= a ? a : b); }
template <typename T> T min(T a, T b) { return (a <= b ? a : b); }
template <typename T> bool setmin(T &a, T b) {
  if (a <= b) {
    return false;
  } else {
    a = b;
    return true;
  }
}
template <typename T> bool setmax(T &a, T b) {
  if (b <= a) {
    return false;
  } else {
    a = b;
    return true;
  }
}
template <typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

int n, m;
LL dp[2][301][301][2];
const int MOD = 1000000007;

int main() {
  scanf("%d%d", &n, &m);

  int from = 0, to = 1;
  dp[to][1][1][0] = 1;
  inc(mm, m) {
    swap(from, to);

    inc1(i, n) {
      inc1(j, n) {
        dp[to][i][j][0] = 0;
        dp[to][i][j][1] = 0;
      }
    }

    inc1(i, n) {
      inc1(j, n) {
        LL zero = dp[from][i][j][0];
        LL one = dp[from][i][j][1];

        (dp[to][i][j][0] += zero * j) %= MOD;
        (dp[to][i][j + 1][1] += zero * (i - j)) %= MOD;
        (dp[to][i + 1][j][1] += zero * (n - i)) %= MOD;
        (dp[to][i][i][0] += one * j) %= MOD;
        (dp[to][i][j][1] += one * (i - j)) %= MOD;
        (dp[to][i + 1][j][1] += one * (n - i)) %= MOD;
      }
    }
  }

  printf("%lld\n", dp[to][n][n][0]);

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>

typedef long long signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for (int i = (l); i < (r); i++)
#define incII(i, l, r) for (int i = (l); i <= (r); i++)
#define decID(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define decII(i, l, r) for (int i = (r); i >= (l); i--)
#define inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) < (r))

template <typename T> void swap(T &x, T &y) {
  T t = x;
  x = y;
  y = t;
  return;
}
template <typename T> T abs(T x) { return (0 <= x ? x : -x); }
template <typename T> T max(T a, T b) { return (b <= a ? a : b); }
template <typename T> T min(T a, T b) { return (a <= b ? a : b); }
template <typename T> bool setmin(T &a, T b) {
  if (a <= b) {
    return false;
  } else {
    a = b;
    return true;
  }
}
template <typename T> bool setmax(T &a, T b) {
  if (b <= a) {
    return false;
  } else {
    a = b;
    return true;
  }
}
template <typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

int n, m;
LL dp[2][302][302][2];
const int MOD = 1000000007;

int main() {
  scanf("%d%d", &n, &m);

  int from = 0, to = 1;
  dp[to][1][1][0] = 1;
  inc(mm, m) {
    swap(from, to);

    inc1(i, n) {
      inc1(j, n) {
        dp[to][i][j][0] = 0;
        dp[to][i][j][1] = 0;
      }
    }

    inc1(i, n) {
      inc1(j, n) {
        LL zero = dp[from][i][j][0];
        LL one = dp[from][i][j][1];

        (dp[to][i][j][0] += zero * j) %= MOD;
        (dp[to][i][j + 1][1] += zero * (i - j)) %= MOD;
        (dp[to][i + 1][j][1] += zero * (n - i)) %= MOD;
        (dp[to][i][i][0] += one * j) %= MOD;
        (dp[to][i][j][1] += one * (i - j)) %= MOD;
        (dp[to][i + 1][j][1] += one * (n - i)) %= MOD;
      }
    }
  }

  printf("%lld\n", dp[to][n][n][0]);

  return 0;
}