Problem p03364 (C++)

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

using namespace std;
using ll = long long;

static const ll MODN = 2;
static const ll P = 321;

vector<ll> mod = {76543, 346072};

vector<vector<ll>> deg;
vector<vector<vector<ll>>> hor, vert;

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<string> ar(n);
  for (int i = 0; i < n; ++i) {
    cin >> ar[i];
  }
  deg.assign(MODN, vector<ll>(n + 1));
  hor.assign(MODN, vector<vector<ll>>(n, vector<ll>(n + 1)));
  vert.assign(MODN, vector<vector<ll>>(n, vector<ll>(n + 1)));
  for (int m = 0; m < MODN; ++m) {
    deg[m][0] = 1;
    for (int i = 0; i < n; ++i) {
      deg[m][i + 1] = (deg[m][i] * P) % mod[m];
    }
  }
  for (int m = 0; m < MODN; ++m) {
    for (int i = 0; i < n; ++i) {
      deg[m][i + 1] = (deg[m][i] * P) % mod[m];
    }
  }
  for (int m = 0; m < MODN; ++m) {
    for (int j = 0; j < n; ++j) {
      for (int i = 0; i < n; ++i) {
        hor[m][j][i + 1] = (hor[m][j][i] * P + ar[j][i]) % mod[m];
        vert[m][j][i + 1] = (vert[m][j][i] * P + ar[i][j]) % mod[m];
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      bool ok = true;
      for (int k = 0; k < n; ++k) {
        int hid = (i + k) % n;
        int vid = (j + k) % n;
        for (int m = 0; m < MODN; ++m) {
          ll hh = (((hor[m][hid][n] - hor[m][hid][j] * deg[m][n - j]) % mod[m] +
                    mod[m]) *
                       deg[m][j] +
                   hor[m][hid][j]) %
                  mod[m];
          ll vv =
              (((vert[m][vid][n] - vert[m][vid][i] * deg[m][n - i]) % mod[m] +
                mod[m]) *
                   deg[m][i] +
               vert[m][vid][i]) %
              mod[m];
          if (hh != vv) {
            ok = false;
            break;
          }
        }
        if (!ok)
          break;
      }
      if (ok) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

static const ll MODN = 1;
static const ll P = 321;

vector<ll> mod = {76543, 346072};

vector<vector<ll>> deg;
vector<vector<vector<ll>>> hor, vert;

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<string> ar(n);
  for (int i = 0; i < n; ++i) {
    cin >> ar[i];
  }
  deg.assign(MODN, vector<ll>(n + 1));
  hor.assign(MODN, vector<vector<ll>>(n, vector<ll>(n + 1)));
  vert.assign(MODN, vector<vector<ll>>(n, vector<ll>(n + 1)));
  for (int m = 0; m < MODN; ++m) {
    deg[m][0] = 1;
    for (int i = 0; i < n; ++i) {
      deg[m][i + 1] = (deg[m][i] * P) % mod[m];
    }
  }
  for (int m = 0; m < MODN; ++m) {
    for (int i = 0; i < n; ++i) {
      deg[m][i + 1] = (deg[m][i] * P) % mod[m];
    }
  }
  for (int m = 0; m < MODN; ++m) {
    for (int j = 0; j < n; ++j) {
      for (int i = 0; i < n; ++i) {
        hor[m][j][i + 1] = (hor[m][j][i] * P + ar[j][i]) % mod[m];
        vert[m][j][i + 1] = (vert[m][j][i] * P + ar[i][j]) % mod[m];
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      bool ok = true;
      for (int k = 0; k < n; ++k) {
        int hid = (i + k) % n;
        int vid = (j + k) % n;
        for (int m = 0; m < MODN; ++m) {
          ll hh = (((hor[m][hid][n] - hor[m][hid][j] * deg[m][n - j]) % mod[m] +
                    mod[m]) *
                       deg[m][j] +
                   hor[m][hid][j]) %
                  mod[m];
          ll vv =
              (((vert[m][vid][n] - vert[m][vid][i] * deg[m][n - i]) % mod[m] +
                mod[m]) *
                   deg[m][i] +
               vert[m][vid][i]) %
              mod[m];
          if (hh != vv) {
            ok = false;
            break;
          }
        }
        if (!ok)
          break;
      }
      if (ok) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}