Problem p02947 (C++)

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

#define fi first
#define se second
#define be begin
#define en end
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ALL(a) (a).be(), (a).en()
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)       // repeat n times
#define REP2(i, s, n) for (int(i) = (s); (i) < (n); (i)++) // repeat from s to n
#define REPD(i, n) for (int(i) = (n); (i) >= 0; (i)--)     // repeat from n to 0
#define REPD2(i, s, e)                                                         \
  for (int(i) = (s); (i) >= (e); (i)--)     // repeat from s to e
#define RANGE(i, v) for (auto &(i) : v)     // repeat range
#define ASIZE(a) (sizeof(a) / sizeof(a[0])) // array size

using LL = long long;

template <typename T> using V = vector<T>;
using Vi = V<int>;
using Vll = V<LL>;
using Vs = V<string>;

template <typename T1, typename T2> using M = map<T1, T2>;
using Mii = M<int, int>;
using Msll = M<string, LL>;

LL factorial(int x, int m = 0) {
  LL a = 1;
  if (m == 0) {
    REPD2(i, x, 1) { a *= i; }
  } else {
    REPD2(i, x, 1) { a = (a * i) % m; }
  }
  return a;
}

int main() {
  int n;
  cin >> n;
  Vs s(n);
  REP(i, n) {
    cin >> s[i];
    sort(ALL(s[i]));
  }

  sort(ALL(s));

  // cout<<endl;
  // REP(i,n){
  //     cout<<s[i]<<endl;
  // }

  Msll s_num;
  REP(i, n) { s_num[s[i]]++; }

  LL cnt = 0;
  for (auto m : s_num) {
    // cout<<m.fi<<" : "<<m.se<<endl;
    if (m.se == 1)
      continue;
    LL fact = factorial(m.se);
    LL m_2 = factorial(m.se - 2);
    // cout<<fact<<" : "<<m_2<<endl;
    cnt += fact / (m_2 * 2);
  }

  cout << cnt << endl;

  return 0;
}

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

#define fi first
#define se second
#define be begin
#define en end
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ALL(a) (a).be(), (a).en()
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)       // repeat n times
#define REP2(i, s, n) for (int(i) = (s); (i) < (n); (i)++) // repeat from s to n
#define REPD(i, n) for (int(i) = (n); (i) >= 0; (i)--)     // repeat from n to 0
#define REPD2(i, s, e)                                                         \
  for (int(i) = (s); (i) >= (e); (i)--)     // repeat from s to e
#define RANGE(i, v) for (auto &(i) : v)     // repeat range
#define ASIZE(a) (sizeof(a) / sizeof(a[0])) // array size

using LL = long long;

template <typename T> using V = vector<T>;
using Vi = V<int>;
using Vll = V<LL>;
using Vs = V<string>;

template <typename T1, typename T2> using M = map<T1, T2>;
using Mii = M<int, int>;
using Msll = M<string, LL>;

LL factorial(int x, int m = 0) {
  LL a = 1;
  if (m == 0) {
    REPD2(i, x, 1) { a *= i; }
  } else {
    REPD2(i, x, 1) { a = (a * i) % m; }
  }
  return a;
}

int main() {
  int n;
  cin >> n;
  Vs s(n);
  REP(i, n) {
    cin >> s[i];
    sort(ALL(s[i]));
  }

  sort(ALL(s));

  // cout<<endl;
  // REP(i,n){
  //     cout<<s[i]<<endl;
  // }

  Msll s_num;
  REP(i, n) { s_num[s[i]]++; }

  LL cnt = 0;
  for (auto m : s_num) {
    // cout<<m.fi<<" : "<<m.se<<endl;
    if (m.se == 1)
      continue;
    cnt += m.se * (m.se - 1) / 2;
  }

  cout << cnt << endl;

  return 0;
}