Problem p02947 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define Rep(i, m, n) for (int i = m; i < n; i++)
#define repeatrev(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define itn int
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
typedef priority_queue<int> Pr;
const int MOD = 9999973;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
// 二項係数の計算をしたい時用

const int MAX = 5100000;

unsigned long long fac[MAX], finv[MAX], inv[MAX];
unsigned ll kaijyo(ll n) {
  if (n == 0)
    return 1;
  return n * kaijyo(n - 1);
}

unsigned ll ansans(ll n, ll k) { return kaijyo(n) / kaijyo(n - k) / kaijyo(k); }
ll fact[MAX], fact_inv[MAX];

// 繰り返し二乗法
ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

ll comb(ll n, ll r) { return (fact[n] * fact_inv[r]) * fact_inv[n - r]; }

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  COMinit();
  int n;
  cin >> n;
  int offset;
  offset = 'a';
  int a[n][29];
  rep(i, n) { rep(j, 29) a[i][j] = 0; }
  rep(i, n) {
    string s;
    cin >> s;
    rep(j, 10) { a[i][s[j] - offset]++; }
  }
  unsigned ll ans = 0;
  map<string, int> dict;
  rep(i, n) {
    int g = 0;
    int hh = 0;
    string s = "          ";
    int place = 0;
    rep(j, 26) {
      int gg = 0;
      gg = a[i][j];
      while (gg != 0) {
        s[hh] = (char)(offset + j);
        hh++;
        // cout << s[i] << endl;
        // s[i] = SS;
        gg--;
      }
    }
    // cout << s << endl;
    dict[s]++;
  }
  /*

  if(g == 0){

  }
}

rep(i, n - 1){
  Rep(j, i + 1, n){
      int g = 0;
      rep(s, 29){
          if(a[i][s] != a[j][s]) g = 1;
      }
      if(g == 0){
          //cout << i << " " << j << endl;
          ans ++;
      }
  }
}*/

  for (auto i = dict.begin(); i != dict.end(); ++i) {
    int ggg = i->second;
    // cout << i->first << " " << ggg << endl;
    // cout << ggg << endl;
    if (ggg >= 2)
      ans += ansans(ggg, 2);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define Rep(i, m, n) for (int i = m; i < n; i++)
#define repeatrev(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define itn int
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
typedef priority_queue<int> Pr;
const int MOD = 9999973;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
// 二項係数の計算をしたい時用

const int MAX = 5100000;

unsigned long long fac[MAX], finv[MAX], inv[MAX];
unsigned ll kaijyo(ll n) {
  if (n == 0)
    return 1;
  return n * kaijyo(n - 1);
}

unsigned ll ansans(ll n, ll k) { return kaijyo(n) / kaijyo(n - k) / kaijyo(k); }
ll fact[MAX], fact_inv[MAX];

// 繰り返し二乗法
ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

ll comb(ll n, ll r) { return (fact[n] * fact_inv[r]) * fact_inv[n - r]; }

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  COMinit();
  int n;
  cin >> n;
  int offset;
  offset = 'a';
  int a[n][29];
  rep(i, n) { rep(j, 29) a[i][j] = 0; }
  rep(i, n) {
    string s;
    cin >> s;
    rep(j, 10) { a[i][s[j] - offset]++; }
  }
  unsigned ll ans = 0;
  map<string, int> dict;
  rep(i, n) {
    int g = 0;
    int hh = 0;
    string s = "          ";
    int place = 0;
    rep(j, 26) {
      int gg = 0;
      gg = a[i][j];
      while (gg != 0) {
        s[hh] = (char)(offset + j);
        hh++;
        // cout << s[i] << endl;
        // s[i] = SS;
        gg--;
      }
    }
    // cout << s << endl;
    dict[s]++;
  }
  /*

  if(g == 0){

  }
}

rep(i, n - 1){
  Rep(j, i + 1, n){
      int g = 0;
      rep(s, 29){
          if(a[i][s] != a[j][s]) g = 1;
      }
      if(g == 0){
          //cout << i << " " << j << endl;
          ans ++;
      }
  }
}*/

  for (auto i = dict.begin(); i != dict.end(); ++i) {
    int ggg = i->second;
    // cout << i->first << " " << ggg << endl;
    // cout << ggg << endl;
    if (ggg >= 2) {
      if (ggg % 2 == 0) {
        unsigned ll ffff = ggg / 2;
        ans += ffff * (ggg - 1);
      } else {
        unsigned ll ffff = (ggg - 1) / 2;
        ans += ffff * (ggg);
      }
      // ans += ggg * (ggg - 1) / 2;
    }
  }
  cout << ans << endl;
}