prithivMLmods/Coder-Stat huggingface dataset:
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const int N = 305;
bool dp[N][N][N];
string s[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.setf(cout.fixed);
  cout.precision(20);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    s[i] += s[i];
    s[i].pop_back();
    reverse(s[i].begin(), s[i].end());
  }
  for (int i = n; i < n + n - 1; i++) {
    s[i] = s[i - n];
  }
  for (int i = 0; i < n + n - 1; i++) {
    for (int j = 0; j < n + n - 1; j++) {
      dp[i][j][0] = true;
    }
  }
  for (int k = 1; k < n; k++) {
    for (int i = 0; i + k < n + n - 1; i++) {
      for (int j = 0; j + k < n + n - 1; j++) {
        dp[i][j][k] = dp[i + 1][j][k - 1] && dp[i][j + 1][k - 1] &&
                      s[i][j] == s[i + k][j + k];
      }
    }
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      res += dp[i][j][n - 1];
    }
  }
  cout << res << "\n";
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const int N = 605;
bool dp[N][N][N];
string s[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.setf(cout.fixed);
  cout.precision(20);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    s[i] += s[i];
    s[i].pop_back();
    reverse(s[i].begin(), s[i].end());
  }
  for (int i = n; i < n + n - 1; i++) {
    s[i] = s[i - n];
  }
  for (int i = 0; i < n + n - 1; i++) {
    for (int j = 0; j < n + n - 1; j++) {
      dp[i][j][0] = true;
    }
  }
  for (int k = 1; k < n; k++) {
    for (int i = 0; i + k < n + n - 1; i++) {
      for (int j = 0; j + k < n + n - 1; j++) {
        dp[i][j][k] = dp[i + 1][j][k - 1] && dp[i][j + 1][k - 1] &&
                      s[i][j] == s[i + k][j + k];
      }
    }
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      res += dp[i][j][n - 1];
    }
  }
  cout << res << "\n";
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#define SF scanf
#define PF printf
#define MAXN 310
using namespace std;
int n, ans;
char s[MAXN][MAXN];
bool used[MAXN];
int main() {
  SF("%d", &n);
  for (int i = 0; i < n; i++)
    SF("%s", s[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++)
        if (s[i][j] != s[(j - (k + i - j) + 2 * n) % n][(i + (k + i - j)) % n])
          used[k + i - j] = 1;
    }
  for (int i = 0; i < n; i++)
    if (used[i] == 0)
      ans += n;
  PF("%d", ans);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#define SF scanf
#define PF printf
#define MAXN 310
using namespace std;
int n, ans;
char s[MAXN][MAXN];
bool used[MAXN];
int main() {
  SF("%d", &n);
  for (int i = 0; i < n; i++)
    SF("%s", s[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++)
        if (s[i][j] !=
            s[(j - (k + i - j) + 2 * n) % n][(i + (k + i - j) + n) % n])
          used[(k + i - j + n) % n] = 1;
    }
  for (int i = 0; i < n; i++)
    if (used[i] == 0)
      ans += n;
  PF("%d", ans);
}
Problem p03364 (Python)

Buggy submission (status: Time Limit Exceeded):
import numpy as np

n = int(input())
s = np.array([list(map(ord, input())) for _ in range(n)])
s = np.hstack((s, s))
s = np.vstack((s, s))
ans = 0
for i in range(n):
    for j in range(n):
        p = s[i : i + n, j : j + n]
        if (p == p.T).all():
            ans += 1

print(ans)


Fixed submission:
import numpy as np

n = int(input())
s = np.array([list(map(ord, input())) for _ in range(n)])
s = np.hstack((s, s))
s = np.vstack((s, s))
ans = 0
for i in range(n):
    p = s[0:n, i : i + n]
    if (p == p.T).all():
        ans += n

print(ans)
Problem p03364 (Python)

Buggy submission (status: Time Limit Exceeded):
# 入力
N = int(input())
S = [list(input()) for _ in range(N)]

# 解は、B = 0として、A = 0, 1, ..., N - 1 のうち、よい盤面になるものの個数のN倍
ans = N * sum(
    all(S[i][j] == S[(j - A) % N][(i + A) % N] for i in range(N) for j in range(i + 1))
    for A in range(N)
)

# 出力
print(ans)


Fixed submission:
# 入力
N = int(input())
S = [list(input()) for _ in range(N)]

# 解は、B = 0として、A = 0, 1, ..., N - 1 のうち、よい盤面になるものの個数のN倍
ans = N * sum(
    all(S[i][j] == S[(j - A) % N][(i + A) % N] for i in range(N) for j in range(N))
    for A in range(N)
)

# 出力
print(ans)
Problem p03364 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>

typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;

int N;
string S[300];

bool check(int i, int j) {
  FOR(di, 1, N)
  REP(dj, di) if (S[(i + di) % N][(j + dj) % N] !=
                  S[(i + dj) % N][(j + di) % N]) return false;
  return true;
}

int main() {
  cin >> N;
  REP(i, N) cin >> S[i];

  int count = 0;
  REP(i, N) REP(j, N) if (check(i, j)) count++;
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;

int N;
string S[300];

bool check(int i, int j) {
  FOR(di, 1, N)
  REP(dj, di) if (S[(i + di) % N][(j + dj) % N] !=
                  S[(i + dj) % N][(j + di) % N]) return false;
  return true;
}

int main() {
  cin >> N;
  REP(i, N) cin >> S[i];

  int count = 0;
  REP(i, N)
  if (check(i, 0))
    count += N;
  cout << count << endl;
  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
const int MAXN = 300 + 10;
string mat[MAXN], sec[MAXN];
int n;
bool check(int x) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      sec[i][j] = mat[(i + x) % n][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (sec[i][j] != sec[j][i])
        return false;
    }
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> mat[i];
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (check(i))
      ret += n;
  }
  cout << ret << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
const int MAXN = 300 + 10;
string mat[MAXN], sec[MAXN];
int n;
bool check(int x) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      sec[i][j] = mat[(i + x) % n][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (sec[i][j] != sec[j][i])
        return false;
    }
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> mat[i];
    sec[i] = mat[i];
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (check(i))
      ret += n;
  }
  cout << ret << endl;

  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define LL long long
#define PB push_back
#define MP make_pair

int main() {
  int N;
  string S[300];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> S[i];
  int ans = 0;
  for (int i = 0; i < N; i++) {
    bool flg = true;
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        if (S[j][k] != S[k][j])
          flg = false;
      }
    }
    ans += flg * N;
    string s = S[0];
    for (int j = 0; j < N - 1; j++) {
      S[i] = S[i + 1];
    }
    S[N - 1] = s;
  }
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define LL long long
#define PB push_back
#define MP make_pair

int main() {
  int N;
  string S[300];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> S[i];
  int ans = 0;
  for (int i = 0; i < N; i++) {
    bool flg = true;
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        if (S[j][k] != S[k][j])
          flg = false;
      }
    }
    ans += flg * N;
    string s = S[0];
    for (int j = 0; j < N - 1; j++) {
      S[j] = S[j + 1];
    }
    S[N - 1] = s;
  }
  cout << ans << endl;
}
Problem p03364 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int N;
char S[301][301];
int sum;

bool check(int A, int B) {
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (S[(i + A) % N][(j + B) % N] != S[(j + A) % N][(i + B) % N]) {
        return false;
      }
    }
  }
  return true;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> S[i][j];
    }
  }

  for (int A = 0; A < N; ++A) {
    for (int B = 0; B < N; ++B) {
      if (check(A, B)) {
        ++sum;
      }
    }
  }
  cout << sum << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
int N;
char S[301][301];
int sum;

bool check(int A, int B) {
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (S[(i + A) % N][(j + B) % N] != S[(j + A) % N][(i + B) % N]) {
        return false;
      }
    }
  }
  return true;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> S[i][j];
    }
  }

  for (int A = 0; A < N; ++A) {
    if (check(A, 0)) {
      sum += N;
    }
  }
  cout << sum << endl;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);

int main() {
  ll n;
  cin >> n;
  vector<string> s;
  rep(i, n) {
    string ss;
    cin >> ss;
    s.push_back(ss + ss);
  }
  //    rep(i,s.size()){
  //        cout<<s[i]<<endl;
  //   }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    bool f = true;
    for (int si = i; si < i + n; si++) {
      for (int sj = 0; sj < n; sj++) {
        if ((si - i) == (sj)) {
        } else {
          if (s[si][sj] != s[si][sj + i]) {
            f = false;
            break;
          }
        }
        if (!f)
          break;
      }
    }
    if (f)
      ans++;
  }
  cout << ans * n << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);

int main() {
  ll n;
  cin >> n;
  vector<string> s;
  rep(i, n) {
    string ss;
    cin >> ss;
    s.push_back(ss + ss);
  }
  //    rep(i,s.size()){
  //        cout<<s[i]<<endl;
  //   }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    bool f = true;
    for (int si = i; si < i + n; si++) {
      for (int sj = 0; sj < n; sj++) {
        if ((si - i) == (sj)) {
        } else {
          if (s[sj][si] != s[si - i][sj + i]) {
            f = false;
            break;
          }
        }
        if (!f)
          break;
      }
    }
    if (f)
      ans++;
  }
  cout << ans * n << endl;

  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string a[300], b[300];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int ans = 0;
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        b[i][j] = a[i][(j + k) % n];
    bool ok = true;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (b[i][j] != b[j][i])
          ok = false;
    ans += ok;
  }
  cout << ans * n << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string a[300], b[300];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i], b[i].resize(n);
  int ans = 0;
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        b[i][j] = a[i][(j + k) % n];
    bool ok = true;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (b[i][j] != b[j][i])
          ok = false;
    ans += ok;
  }
  cout << ans * n << endl;
}
Problem p03364 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int N = 6e2 + 10;
char a[N][N];
int vis[N][N];
inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
  int n = read();
  For(i, 1, n) For(j, 1, n) {
    char ch = getchar();
    while (!isalpha(ch))
      ch = getchar();
    a[i][j] = ch;
  }
  For(i, 1, (n << 1) - 1) For(j, 1, (n << 1) - 1) a[i][j] =
      a[(i - 1) % n + 1][(j - 1) % n + 1];
  int ans = 0;
  For(i, 0, n - 1) For(j, 0, n - 1) {
    if (i == 0 || j == 0) {
      int Flag = 0;
      For(k, 1, n) For(l, 1, n) if (a[k + i][l + j] ^ a[l + i][k + j]) Flag = 1;
      if (!Flag)
        ans++, vis[i][j] = 1;
    } else if (vis[i - 1][j - 1])
      ans++, vis[i][j] = 1;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int N = 6e2 + 10;
char a[N][N];
int vis[N][N];
inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
int main() {
  int n = read();
  For(i, 1, n) For(j, 1, n) {
    char ch = getchar();
    while (!isalpha(ch))
      ch = getchar();
    a[i][j] = ch;
  }
  For(i, 1, (n << 1) - 1) For(j, 1, (n << 1) - 1) a[i][j] =
      a[(i - 1) % n + 1][(j - 1) % n + 1];
  int ans = 0;
  For(i, 0, n - 1) For(j, 0, n - 1) {
    if (i == 0 || j == 0) {
      int Flag = 0;
      For(k, 1, n) For(l, 1, n) if (a[k + i][l + j] ^ a[l + i][k + j]) Flag = 1;
      if (!Flag)
        ans++, vis[i][j] = 1;
    } else if (vis[i - 1][j - 1])
      ans++, vis[i][j] = 1;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int N;
char fid[310][310];
int ans;

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      char c;
      scanf(" %c", &c);
      fid[i][j] = c;
      fid[i + N][j] = c;
      fid[i][j + N] = c;
      fid[i + N][j + N] = c;
    }
  }

  for (int k = 0; k < N; k++) {
    int h = k, w = 0;
    bool flag = true;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < i; j++) {
        if (fid[(h + i) % N][(w + j) % N] != fid[(h + j) % N][(w + i) % N])
          flag = false;
      }
    }
    if (flag)
      ans += N - k;
  }
  for (int k = 1; k < N; k++) {
    int h = 0, w = k;
    bool flag = true;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (fid[(h + i) % N][(w + j) % N] != fid[(h + j) % N][(w + i) % N])
          flag = false;
      }
    }
    if (flag)
      ans += N - k;
  }

  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int N;
char fid[610][610];
int ans;

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      char c;
      scanf(" %c", &c);
      fid[i][j] = c;
      fid[i + N][j] = c;
      fid[i][j + N] = c;
      fid[i + N][j + N] = c;
    }
  }

  for (int k = 0; k < N; k++) {
    int h = k, w = 0;
    bool flag = true;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < i; j++) {
        if (fid[(h + i) % N][(w + j) % N] != fid[(h + j) % N][(w + i) % N])
          flag = false;
      }
    }
    if (flag)
      ans += N - k;
  }
  for (int k = 1; k < N; k++) {
    int h = 0, w = k;
    bool flag = true;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (fid[(h + i) % N][(w + j) % N] != fid[(h + j) % N][(w + i) % N])
          flag = false;
      }
    }
    if (flag)
      ans += N - k;
  }

  printf("%d\n", ans);
  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
bool check(int N, const vector<int> &M, int A, int B) {
  for (int i = 0; i < N; ++i) { // ii=0~N
    for (int j = 0; j < N; ++j) {
      int i_ = (i + A) % N;
      int j_ = (j + B) % N;
      int i_t = (j + A) % N;
      int j_t = (i + B) % N;
      if (M[i_ * N + j_] != M[i_t * N + j_t])
        return false;
    }
  }
  // cerr << A << " " << B << endl;
  return true;
}
int main(int argc, char *argv[]) {
  int N;
  cin >> N;
  vector<int> M(N * N);
  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < N; ++j) {
      M[i * N + j] = s[j];
    }
  }
  int count = 0;
  for (int a = 0; a < N; ++a) {
    for (int b = 0; b < N; ++b) {
      count += check(N, M, a, b) ? 1 : 0;
    }
  }
  cout << count;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
bool check(int N, const vector<int> &M, int A, int B) {
  for (int i = 0; i < N; ++i) { // ii=0~N
    for (int j = 0; j < N; ++j) {
      int i_ = (i + A) % N;
      int j_ = (j + B) % N;
      int i_t = (j + A) % N;
      int j_t = (i + B) % N;
      if (M[i_ * N + j_] != M[i_t * N + j_t])
        return false;
    }
  }
  // cerr << A << " " << B << endl;
  return true;
}
int main(int argc, char *argv[]) {
  int N;
  cin >> N;
  vector<int> M(N * N);
  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < N; ++j) {
      M[i * N + j] = s[j];
    }
  }
  int count = 0;
  for (int a = 0; a < N; ++a) {
    count += check(N, M, a, 0) ? N : 0;
  }
  cout << count;
  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 2001, INF = 1 << 30;

int main() {

  int N;
  cin >> N;
  vector<vector<char>> S(N, vector<char>(N));

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> S[i][j];
    }
  }

  int cnt = 0;

  for (int k = 0; k < N; k++) {
    bool flag = true;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (S[(i + k) & N][j] != S[(j + k) % N][i])
          flag = false;
      }
    }
    if (flag)
      cnt++;
  }

  cout << cnt * N << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 2001, INF = 1 << 30;

int main() {

  int N;
  cin >> N;
  vector<vector<char>> S(N, vector<char>(N));

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> S[i][j];
    }
  }

  int cnt = 0;

  for (int k = 0; k < N; k++) {
    bool flag = true;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (S[(i + k) % N][j] != S[(j + k) % N][i])
          flag = false;
      }
    }
    if (flag)
      cnt++;
  }

  cout << cnt * N << endl;
}
Problem p03364 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 600 + 10;

int n;
char s[maxn][maxn];

int main() {
  //	freopen("data.in","r",stdin);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      s[i][j + n] = s[i][j];
      s[i + n][j] = s[i][j];
      s[i + n][j + n] = s[i][j];
    }
  }
  int cnt = 0, Ans = 0;
  for (int del = -n + 1; del <= n - 1; del++) {
    int flag = 0;
    cnt = 0;
    for (int y = max(1, -del + 1); y <= n && del + y <= n; y++) {
      int x = del + y;
      if (!flag) {
        for (int i = 1; i <= n; i++) {
          for (int j = 1; j < i; j++) {
            if (s[x + i - 1][y + j - 1] != s[x + j - 1][y + i - 1])
              cnt++;
          }
        }
      } else {
        for (int i = 1; i <= n; i++) {
          if (s[x - 1][y - 1 + i - 1] != s[x - 1 + i - 1][y - 1])
            cnt--;
        }
        for (int i = 1; i <= n; i++) {
          if (s[x + i - 1][y + n - 1] != s[x + n - 1][y + i - 1])
            cnt++;
        }
      }
      if (!cnt)
        Ans++;
    }
  }
  printf("%d\n", Ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 600 + 10;

int n;
char s[maxn][maxn];

int main() {
  //	freopen("data.in","r",stdin);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      s[i][j + n] = s[i][j];
      s[i + n][j] = s[i][j];
      s[i + n][j + n] = s[i][j];
    }
  }
  int cnt = 0, Ans = 0;
  for (int del = -n + 1; del <= n - 1; del++) {
    int flag = 0;
    cnt = 0;
    for (int y = max(1, -del + 1); y <= n && del + y <= n; y++) {
      int x = del + y;
      if (!flag) {
        for (int i = 1; i <= n; i++) {
          for (int j = 1; j < i; j++) {
            if (s[x + i - 1][y + j - 1] != s[x + j - 1][y + i - 1])
              cnt++;
          }
        }
        flag = 1;
      } else {
        for (int i = 1; i <= n; i++) {
          if (s[x - 1][y - 1 + i - 1] != s[x - 1 + i - 1][y - 1])
            cnt--;
        }
        for (int i = 1; i <= n; i++) {
          if (s[x + i - 1][y + n - 1] != s[x + n - 1][y + i - 1])
            cnt++;
        }
      }
      if (!cnt)
        Ans++;
    }
  }
  printf("%d\n", Ans);
  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> PA;
typedef priority_queue<int, vector<int>, greater<int>> PQG;
#define REP(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define fi first
#define se second
#define pb push_back
#define INF 1000000009
#define MOD 100000
#define MAXN 102
#define MAXC 100000000000014
#define MAXV 1000000009

int n, ans;
char s[2 * MAXN][2 * MAXN];

int main() {
  cin >> n;
  REP(i, 0, n) {
    REP(j, 0, n) {
      cin >> s[i][j];
      s[i][j + n] = s[i][j];
      s[i + n][j] = s[i][j];
      s[i + n][j + n] = s[i][j];
    }
  }
  REP(k, 0, n) {
    int same = 0;
    REP(i, 0, n) {
      REP(j, 0, n) {
        if (s[i][j + k] == s[j][i + k]) {
          same++;
        }
      }
    }
    if (same == n * n)
      ans++;
  }
  cout << ans * n << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> PA;
typedef priority_queue<int, vector<int>, greater<int>> PQG;
#define REP(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define fi first
#define se second
#define pb push_back
#define INF 1000000009
#define MOD 100000
#define MAXN 302
#define MAXC 100000000000014
#define MAXV 1000000009

int n, ans;
char s[2 * MAXN][2 * MAXN];

int main() {
  cin >> n;
  REP(i, 0, n) {
    REP(j, 0, n) {
      cin >> s[i][j];
      s[i][j + n] = s[i][j];
      s[i + n][j] = s[i][j];
      s[i + n][j + n] = s[i][j];
    }
  }
  REP(k, 0, n) {
    int same = 0;
    REP(i, 0, n) {
      REP(j, 0, n) {
        if (s[i][j + k] == s[j][i + k]) {
          same++;
        }
      }
    }
    if (same == n * n)
      ans++;
  }
  cout << ans * n << endl;

  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string a[333];
string b[333];
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll res = 0;
  for (int k = 0; k < n; k++) {
    int flag = 1;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        b[(i + k) % n][j] = a[i][j];
      }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (i != j && b[i][j] != b[j][i])
          flag = 0;
    if (flag)
      res += n;
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string a[333];
string b[333];
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i], b[i] = a[i];
  ll res = 0;
  for (int k = 0; k < n; k++) {
    int flag = 1;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        b[(i + k) % n][j] = a[i][j];
      }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (i != j && b[i][j] != b[j][i])
          flag = 0;
    if (flag)
      res += n;
  }
  cout << res << endl;
  return 0;
}
Problem p03365 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rint register int
#define ll long long
using namespace std;
const int mod = 1e9 + 7;
ll ksm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
ll fac[1000100], Ifac[1010000];
inline ll C(ll x, ll y) { return fac[x] * Ifac[y] % mod * Ifac[x - y] % mod; }
int n;
int main() {
  cin >> n;
  fac[0] = Ifac[0] = 1;
  for (rint i = 1; i <= 2 * n; ++i)
    fac[i] = fac[i - 1] * i % mod;
  Ifac[2 * n] = ksm(fac[2 * n], mod - 2);
  for (rint i = 2 * n - 1; i; --i)
    Ifac[i] = Ifac[i + 1] * (i + 1) % mod;
  ll ans = 0, las = 0;
  for (rint i = (n + 1) / 2; i <= n - 1; ++i) {
    ll tmp = 1ll * C(i - 1, n - i - 1) * fac[i] % mod * fac[n - 1 - i] % mod;
    ans = ans + (tmp - las) * i % mod + mod;
    ans %= mod;
    las = tmp;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rint register int
#define ll long long
using namespace std;
const int mod = 1e9 + 7;
ll ksm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
ll fac[3000100], Ifac[3010000];
inline ll C(ll x, ll y) { return fac[x] * Ifac[y] % mod * Ifac[x - y] % mod; }
int n;
int main() {
  cin >> n;
  fac[0] = Ifac[0] = 1;
  for (rint i = 1; i <= 2 * n; ++i)
    fac[i] = fac[i - 1] * i % mod;
  Ifac[2 * n] = ksm(fac[2 * n], mod - 2);
  for (rint i = 2 * n - 1; i; --i)
    Ifac[i] = Ifac[i + 1] * (i + 1) % mod;
  ll ans = 0, las = 0;
  for (rint i = (n + 1) / 2; i <= n - 1; ++i) {
    ll tmp = 1ll * C(i - 1, n - i - 1) * fac[i] % mod * fac[n - 1 - i] % mod;
    ans = ans + (tmp - las) * i % mod + mod;
    ans %= mod;
    las = tmp;
  }
  cout << ans;
  return 0;
}
Problem p03365 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

inline int read() {
  int f = 1;
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f = (ch == '-') ? -1 : 1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}

const int N = 1e6 + 5;
const int P = 1e9 + 7;

inline int mul(int a, int b) { return (long long)a * b % P; }
inline int sub(int a, int b) { return a - b < 0 ? a - b + P : a - b; }
inline int add(int a, int b) { return a + b >= P ? a + b - P : a + b; }
inline int modexp(int a, int x) {
  int ret = 1;
  while (x) {
    if (x & 1) {
      ret = mul(ret, a);
    }
    a = mul(a, a);
    x >>= 1;
  }
  return ret;
}

int n;
int fac[N];
int inv[N];

inline int calc(int m, int n) {
  int k = 2 * m - n;
  if (k >= 0 && m - k >= 0) {
    return mul(fac[m], mul(inv[k], inv[m - k]));
  } else {
    return 0;
  }
}

inline int C(int n, int m) { return mul(fac[n], mul(inv[m], inv[n - m])); }

int main() {
  n = read();
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fac[i] = mul(fac[i - 1], i);
  }
  inv[n] = modexp(fac[n], P - 2);
  for (int i = n - 1; i >= 0; --i) {
    inv[i] = mul(inv[i + 1], i + 1);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans = add(ans, mul(fac[n - i - 1],
                       mul(sub(C(n - 1, i), C(i - 1, n - i - 1)), fac[i])));
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

inline int read() {
  int f = 1;
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f = (ch == '-') ? -1 : 1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}

const int N = 1e6 + 5;
const int P = 1e9 + 7;

inline int mul(int a, int b) { return (long long)a * b % P; }
inline int sub(int a, int b) { return a - b < 0 ? a - b + P : a - b; }
inline int add(int a, int b) { return a + b >= P ? a + b - P : a + b; }
inline int modexp(int a, int x) {
  int ret = 1;
  while (x) {
    if (x & 1) {
      ret = mul(ret, a);
    }
    a = mul(a, a);
    x >>= 1;
  }
  return ret;
}

int n;
int fac[N];
int inv[N];

inline int calc(int m, int n) {
  int k = 2 * m - n;
  if (k >= 0 && m - k >= 0) {
    return mul(fac[m], mul(inv[k], inv[m - k]));
  } else {
    return 0;
  }
}

inline int C(int n, int m) { return mul(fac[n], mul(inv[m], inv[n - m])); }

int main() {
  n = read();
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fac[i] = mul(fac[i - 1], i);
  }
  inv[n] = modexp(fac[n], P - 2);
  for (int i = n - 1; i >= 0; --i) {
    inv[i] = mul(inv[i + 1], i + 1);
  }
  int ans = mul(fac[n - 1], n);
  for (int i = (n + 1) / 2; i < n; ++i) {
    ans = sub(ans, mul(fac[n - i - 1], mul(C(i - 1, n - i - 1), fac[i])));
  }

  cout << ans << endl;
  return 0;
}
Problem p03365 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

inline int inv(int n, int p) {
  int i = 1;
  for (int r = n; r > 1; r = p % r)
    i = p / r * long(p - i) % p;
  return i;
}
int f[111111], g[111111];
int main() {
  int n;
  cin >> n;
  int p = 1e9 + 7;
  f[0] = 1, g[n - 1] = 1;
  for (int i = 1; i < n; i++) {
    f[i] = long(i) * f[i - 1] % p;
    g[n - 1 - i] = long(n - 1 - i) * g[n - i] % p;
  }
  int a = 0;
  for (int i = (n + 1) / 2; i < n - 1; i++)
    a = (a + (long(f[i]) * f[i - 1]) % p * g[i * 2 - n + 1]) % p;
  cout << (long(n - 1) * f[n - 1] + long(p - a) * inv(f[n - 2], p)) % p << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

inline int inv(int n, int p) {
  int i = 1;
  for (int r = n; r > 1; r = p % r)
    i = p / r * long(p - i) % p;
  return i;
}
int f[1111111], g[1111111];
int main() {
  int n;
  cin >> n;
  int p = 1e9 + 7;
  f[0] = 1, g[n - 1] = 1;
  for (int i = 1; i < n; i++) {
    f[i] = long(i) * f[i - 1] % p;
    g[n - 1 - i] = long(n - 1 - i) * g[n - i] % p;
  }
  int a = 0;
  for (int i = (n + 1) / 2; i < n - 1; i++)
    a = (a + (long(f[i]) * f[i - 1]) % p * g[i * 2 - n + 1]) % p;
  cout << (long(n - 1) * f[n - 1] + long(p - a) * inv(f[n - 2], p)) % p << endl;
  return 0;
}
Problem p03365 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define MOD 1000000007
typedef long long ll;
ll fact[1000005], inv[1000005];
inline ll quick_pow(ll a, int n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}
inline ll C(int n, int m) { return fact[n] * inv[m] % MOD * inv[n - m] % MOD; }
int main() {
  // freopen("AGC023-C.in", "r", stdin);
  int n;
  scanf("%d", &n);
  ll ans = 0;
  fact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = fact[i - 1] * i % MOD;
  inv[n] = quick_pow(fact[n], MOD - 2);
  for (int i = n; i; i--)
    inv[i - 1] = inv[i] * i % MOD;
  for (int k = 1; k < n; k++)
    (ans += C(k - 1, n - k - 1) * fact[k] % MOD * fact[n - k - 1]) %= MOD;
  printf("%lld\n", (fact[n - 1] * n - ans + MOD) % MOD);
  return 0;
}


Fixed submission:
#include <cstdio>
#define MOD 1000000007
typedef long long ll;
ll fact[1000005], inv[1000005];
inline ll quick_pow(ll a, int n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}
inline ll C(int n, int m) {
  if (n < m)
    return 0;
  return fact[n] * inv[m] % MOD * inv[n - m] % MOD;
}
int main() {
  // freopen("AGC023-C.in", "r", stdin);
  int n;
  scanf("%d", &n);
  ll ans = 0;
  fact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = fact[i - 1] * i % MOD;
  inv[n] = quick_pow(fact[n], MOD - 2);
  for (int i = n; i; i--)
    inv[i - 1] = inv[i] * i % MOD;
  for (int k = 1; k < n; k++)
    (ans += C(k - 1, n - k - 1) * fact[k] % MOD * fact[n - k - 1]) %= MOD;
  printf("%lld\n", (fact[n - 1] * n - ans + MOD) % MOD);
  return 0;
}
Problem p03365 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;

ll N, inf = 1e9 + 7;
ll fact[1000010] = {0};
ll inv[1000010] = {0};

ll mult(ll n, ll m) {
  if (m == 1)
    return n % inf;
  else if (m % 2 == 0) {
    ll t = mult(n, m / 2);
    return (t * t) % inf;
  } else {
    ll t = mult(n, m - 1);
    return (t * n) % inf;
  }
}

void factorial(ll N) {
  for (ll i = 0; i <= N; i++) {
    if (i == 0) {
      fact[i] = 1;
      inv[i] = 1;
    } else {
      fact[i] = (i * fact[i - 1]) % inf;
      inv[i] = mult(fact[i], inf - 2);
    }
  }
}

ll comb(ll n, ll k) {
  if (k == 0 || n == k)
    return 1;
  else
    return (((fact[n] * inv[k]) % inf) * inv[n - k]) % inf;
}

ll hcomb(ll n, ll k) {
  if (n == 0 && k == 0)
    return 1;
  return comb(n + k - 1, k);
}

ll A[100010] = {0};

int main() {
  cin >> N;
  factorial(N);
  if (N == 2)
    cout << 1 << endl;
  else if (N == 3)
    cout << 4 << endl;
  else {
    ll ans = 0;
    for (ll i = 1; i <= N - 1; i++) {
      ll s = comb(i - 1, N - 1 - i), t = (fact[i] * fact[N - 1 - i]) % inf;
      A[i] = (s * t) % inf;
      ll a = A[i] - A[i - 1];
      a = (a + inf) % inf;
      ans += i * a;
      ans %= inf;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;

ll N, inf = 1e9 + 7;
ll fact[1000010] = {0};
ll inv[1000010] = {0};

ll mult(ll n, ll m) {
  if (m == 1)
    return n % inf;
  else if (m % 2 == 0) {
    ll t = mult(n, m / 2);
    return (t * t) % inf;
  } else {
    ll t = mult(n, m - 1);
    return (t * n) % inf;
  }
}

void factorial(ll N) {
  for (ll i = 0; i <= N; i++) {
    if (i == 0) {
      fact[i] = 1;
      inv[i] = 1;
    } else {
      fact[i] = (i * fact[i - 1]) % inf;
      inv[i] = mult(fact[i], inf - 2);
    }
  }
}

ll comb(ll n, ll k) {
  if (k == 0 || n == k)
    return 1;
  else
    return (((fact[n] * inv[k]) % inf) * inv[n - k]) % inf;
}

ll hcomb(ll n, ll k) {
  if (n == 0 && k == 0)
    return 1;
  return comb(n + k - 1, k);
}

ll A[1000010] = {0};

int main() {
  cin >> N;
  factorial(N);
  if (N == 2)
    cout << 1 << endl;
  else if (N == 3)
    cout << 4 << endl;
  else {
    ll ans = 0;
    for (ll i = 1; i <= N - 1; i++) {
      ll s = comb(i - 1, N - 1 - i), t = (fact[i] * fact[N - 1 - i]) % inf;
      A[i] = (s * t) % inf;
      ll a = A[i] - A[i - 1];
      a = (a + inf) % inf;
      ans += i * a;
      ans %= inf;
    }
    cout << ans << endl;
  }
}
Problem p03365 (C++)

Buggy submission (status: Runtime Error):
// khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O2")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 200000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;

ll fac[N], inv[N], dp[N];
int n;

ll POW(ll x, ll t) {
  ll res = 1;
  while (t) {
    if (t & 1)
      res = res * x % MOD;
    t >>= 1;
    x = x * x % MOD;
  }
  return res;
}

ll nCr(ll x, ll y) {
  if (y > x || y < 0 || x < 0)
    return 0;
  ll res = fac[x];
  res = res * inv[y] % MOD;
  res = res * inv[x - y] % MOD;
  return res;
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  fac[0] = inv[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % MOD, inv[i] = POW(fac[i], MOD - 2);
  // cout << nCr(6, 3) << '\n';
  for (int i = 1; i <= n - 1; i++) {
    dp[i] = nCr(i - 1, n - i - 1);
    dp[i] = dp[i] * fac[i] % MOD;
    dp[i] = dp[i] * fac[n - i - 1] % MOD;
  }
  ll sm = 0;
  ll ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    //	cout << dp[i] << '\n';
    dp[i] -= sm;
    dp[i] %= MOD;
    dp[i] += MOD;
    dp[i] %= MOD;
    ans = (ans + dp[i] * i % MOD) % MOD;
    sm = (sm + dp[i]) % MOD;
  }
  cout << ans;

  return 0;
}


Fixed submission:
// khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O2")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1000000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;

ll fac[N], inv[N], dp[N];
int n;

ll POW(ll x, ll t) {
  ll res = 1;
  while (t) {
    if (t & 1)
      res = res * x % MOD;
    t >>= 1;
    x = x * x % MOD;
  }
  return res;
}

ll nCr(ll x, ll y) {
  if (y > x || y < 0 || x < 0)
    return 0;
  ll res = fac[x];
  res = res * inv[y] % MOD;
  res = res * inv[x - y] % MOD;
  return res;
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  fac[0] = inv[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = fac[i - 1] * i % MOD, inv[i] = POW(fac[i], MOD - 2);
  // cout << nCr(6, 3) << '\n';
  for (int i = 1; i <= n - 1; i++) {
    dp[i] = nCr(i - 1, n - i - 1);
    dp[i] = dp[i] * fac[i] % MOD;
    dp[i] = dp[i] * fac[n - i - 1] % MOD;
  }
  ll sm = 0;
  ll ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    //	cout << dp[i] << '\n';
    dp[i] -= sm;
    dp[i] %= MOD;
    dp[i] += MOD;
    dp[i] %= MOD;
    ans = (ans + dp[i] * i % MOD) % MOD;
    sm = (sm + dp[i]) % MOD;
  }
  cout << ans;

  return 0;
}
Problem p03365 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
using namespace std;
typedef long long ll;

ll fact[200000];
ll ppow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll C(ll n, ll k) {
  return fact[n] * ppow(fact[k], MOD - 2) % MOD * ppow(fact[n - k], MOD - 2) %
         MOD;
}

int main() {
  int n;
  scanf("%d", &n);
  if (n == 2) {
    puts("1");
    return 0;
  }
  fact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = (fact[i - 1] * i) % MOD;
  ll cnt = 0, ans = 0;
  for (int k = 2; k <= n - 1; k++) {
    if (k - 1 < n - k - 1)
      continue;
    ll a = C(k - 1, n - k - 1) * fact[k] % MOD * fact[n - k - 1] % MOD;
    (ans += k * (a + MOD - cnt) % MOD) %= MOD;
    cnt = a;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
using namespace std;
typedef long long ll;

ll fact[2000000];
ll ppow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll C(ll n, ll k) {
  return fact[n] * ppow(fact[k], MOD - 2) % MOD * ppow(fact[n - k], MOD - 2) %
         MOD;
}

int main() {
  int n;
  scanf("%d", &n);
  if (n == 2) {
    puts("1");
    return 0;
  }
  fact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = (fact[i - 1] * i) % MOD;
  ll cnt = 0, ans = 0;
  for (int k = 2; k <= n - 1; k++) {
    if (k - 1 < n - k - 1)
      continue;
    ll a = C(k - 1, n - k - 1) * fact[k] % MOD * fact[n - k - 1] % MOD;
    (ans += k * (a + MOD - cnt) % MOD) %= MOD;
    cnt = a;
  }
  cout << ans << endl;
}
Problem p03365 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int mod = 1e9 + 7, maxn = 1e5 + 5;

int fac[maxn], revfac[maxn];
int n;
inline int fastpow(int a, int p) {
  int ans = 1;
  while (p) {
    if (p & 1)
      ans = (ans * a) % mod;
    p >>= 1;
    a = (a * a) % mod;
  }
  return ans;
}
inline int rev(int a) { return fastpow(a, mod - 2); }
void init() {
  revfac[0] = fac[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % mod;
    revfac[i] = rev(fac[i]);
    //		cout<<fac[i]<<' '<<revfac[i]<<endl;
  }
}
inline int C(int n, int m) {
  return 1ll * fac[n] * revfac[m] % mod * revfac[n - m] % mod;
}

void work() {
  int ans = 0, comb = 0, prev = 0;
  for (int k = (n + 1) / 2; k < n; k++) {
    prev = comb;
    comb = C(k - 1, n - k - 1) % mod * fac[k] % mod * fac[n - k - 1] % mod;
    ans += (comb - prev + mod) % mod * k % mod;
    ans %= mod;
    //		cout<<comb<<endl;
  }
  cout << ans << endl;
}
signed main() {
  cin >> n;
  init();
  work();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int mod = 1e9 + 7, maxn = 1e6 + 5;

int fac[maxn], revfac[maxn];
int n;
inline int fastpow(int a, int p) {
  int ans = 1;
  while (p) {
    if (p & 1)
      ans = (ans * a) % mod;
    p >>= 1;
    a = (a * a) % mod;
  }
  return ans;
}
inline int rev(int a) { return fastpow(a, mod - 2); }
void init() {
  revfac[0] = fac[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % mod;
    revfac[i] = rev(fac[i]);
    //		cout<<fac[i]<<' '<<revfac[i]<<endl;
  }
}
inline int C(int n, int m) {
  return 1ll * fac[n] * revfac[m] % mod * revfac[n - m] % mod;
}

void work() {
  int ans = 0, comb = 0, prev = 0;
  for (int k = (n + 1) / 2; k < n; k++) {
    prev = comb;
    comb = C(k - 1, n - k - 1) % mod * fac[k] % mod * fac[n - k - 1] % mod;
    ans += (comb - prev + mod) % mod * k % mod;
    ans %= mod;
    //		cout<<comb<<endl;
  }
  cout << ans << endl;
}
signed main() {
  cin >> n;
  init();
  work();
  return 0;
}
Problem p03365 (C++)

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

typedef long long ll;
const int N = 101000, MOD = 1000000007;

ll qpow(ll a, ll b) {
  ll c = 1;
  for (; b; b >>= 1, a = a * a % MOD)
    if (b & 1)
      c = c * a % MOD;
  return c;
}

int fact[N], ifact[N];

int C(int n, int m) {
  if (n < m)
    return 0;
  return (ll)fact[n] * ifact[m] % MOD * ifact[n - m] % MOD;
}

int f(int n, int k) { return C(n - k + 1, k); }

int n;

int main() {
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = (ll)fact[i - 1] * i % MOD;
  ifact[N - 1] = qpow(fact[N - 1], MOD - 2);
  for (int i = N - 1; i; i--)
    ifact[i - 1] = (ll)ifact[i] * i % MOD;

  scanf("%d", &n);

  int ans = (ll)fact[n - 1] * (n - 1) % MOD;

  for (int k = 1; k < n; k++) {
    ans = (ans - (ll)f(n - 3, k) * fact[n - k - 1] % MOD * fact[k] % MOD) % MOD;
  }

  ans = (ans + MOD) % MOD;
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>

typedef long long ll;
const int N = 2001000, MOD = 1000000007;

ll qpow(ll a, ll b) {
  ll c = 1;
  for (; b; b >>= 1, a = a * a % MOD)
    if (b & 1)
      c = c * a % MOD;
  return c;
}

int fact[N], ifact[N];

int C(int n, int m) {
  if (n < m)
    return 0;
  return (ll)fact[n] * ifact[m] % MOD * ifact[n - m] % MOD;
}

int f(int n, int k) { return C(n - k + 1, k); }

int n;

int main() {
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = (ll)fact[i - 1] * i % MOD;
  ifact[N - 1] = qpow(fact[N - 1], MOD - 2);
  for (int i = N - 1; i; i--)
    ifact[i - 1] = (ll)ifact[i] * i % MOD;

  scanf("%d", &n);

  int ans = (ll)fact[n - 1] * (n - 1) % MOD;

  for (int k = 1; k < n; k++) {
    ans = (ans - (ll)f(n - 3, k) * fact[n - k - 1] % MOD * fact[k] % MOD) % MOD;
  }

  ans = (ans + MOD) % MOD;
  printf("%d\n", ans);

  return 0;
}
Problem p03365 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const int maxn = 1e6 + 10;
LL fac[maxn], inv_fac[maxn], num[maxn];

LL qpow(LL a, LL b) {
  LL ret = 1LL;
  while (b) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}

LL inv(LL x) { return qpow(x, mod - 2); }

LL C(int n, int m) { return fac[n] * inv_fac[m] % mod * inv_fac[n - m] % mod; }

int main() {
  int N;
  scanf("%d", &N);
  if (N == 2)
    return puts("1");
  fac[0] = 1;
  for (int i = 1; i <= N; ++i)
    fac[i] = fac[i - 1] * i % mod;
  inv_fac[N] = inv(fac[N]);
  for (int i = N - 1; i >= 0; --i)
    inv_fac[i] = inv_fac[i + 1] * (i + 1) % mod;
  for (int i = (N + 1) / 2; i <= N - 1; ++i)
    num[i] = C(i - 1, N - 1 - i) * fac[i] % mod * fac[N - 1 - i] % mod;
  LL ans = 0;
  for (int i = N - 1; i >= (N + 1) / 2; i--) {
    num[i] = (num[i] - num[i - 1] + mod) % mod;
    ans = (ans + num[i] * i) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const int maxn = 1e6 + 10;
LL fac[maxn], inv_fac[maxn], num[maxn];

LL qpow(LL a, LL b) {
  LL ret = 1LL;
  while (b) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}

LL inv(LL x) { return qpow(x, mod - 2); }

LL C(int n, int m) { return fac[n] * inv_fac[m] % mod * inv_fac[n - m] % mod; }

int main() {
  int N;
  scanf("%d", &N);
  if (N == 2) {
    puts("1");
    return 0;
  }
  fac[0] = 1;
  for (int i = 1; i <= N; ++i)
    fac[i] = fac[i - 1] * i % mod;
  inv_fac[N] = inv(fac[N]);
  for (int i = N - 1; i >= 0; --i)
    inv_fac[i] = inv_fac[i + 1] * (i + 1) % mod;
  for (int i = (N + 1) / 2; i <= N - 1; ++i)
    num[i] = C(i - 1, N - 1 - i) * fac[i] % mod * fac[N - 1 - i] % mod;
  LL ans = 0;
  for (int i = N - 1; i >= (N + 1) / 2; i--) {
    num[i] = (num[i] - num[i - 1] + mod) % mod;
    ans = (ans + num[i] * i) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}