Problem p02728 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
long waru = (long)1e9 + 7;
vector<long> factroial, finverse, mother, answer;
int c = 0;
long getpower(long a, long p, long base) {
  long result = 1; // d = (long)log2(p);
  vector<long> L;
  for (long i = 1; i < p * 2; i *= 2) {
    long ap = ((p / i) % 2 == 1) ? a : 1;
    result = result * ap % base;
    a = (a * a) % base;
  }
  return result;
}
long counttree(map<long, vector<long>> &path, long pos, long parent,
               vector<long> &d) {
  long result;
  result = 1;
  for (int i = 0; i < path[pos].size(); i++) { //(long x : path[pos]) {
    if (parent != path[pos][i]) {
      result += counttree(path, path[pos][i], pos, d);
    }
  }
  mother[pos] = parent;
  d[pos] = result;
  return result;
}
long rootans(map<long, vector<long>> &path, long pos, long parent,
             vector<long> &d, vector<long> &ts) {
  long result;
  result = factroial[ts[pos] - 1];
  for (long x : path[pos]) {
    if (parent != x) {
      result = result * rootans(path, x, pos, d, ts) % waru;
      result = result * finverse[ts[x]] % waru;
    }
  }
  d[pos] = result;
  return result;
}
void calans(map<long, vector<long>> &path, long pos, long parent,
            vector<long> &ts, long N) {
  long result;
  long ans = factroial[N - 1];
  // rootQA[i]*(rootQA[mother](n-1-size)!size!/n!/rootQA[i])<除いた親
  //*(N-1)!/(size-1)!/(N-)!
  ans = ans * answer[parent] % waru;
  ans = ans * finverse[N - 1] % waru;
  ans = ans * factroial[N - 1 - ts[pos]] % waru;
  ans = ans * factroial[ts[pos]] % waru;
  ans = ans * finverse[ts[pos] - 1] % waru;
  ans = ans * finverse[N - 1 - (ts[pos] - 1)] % waru;
  answer[pos] = ans;
  for (long x : path[pos]) {
    if (parent != x) {
      calans(path, x, pos, ts, N);
    }
  }
}
int main() {
  long N;
  cin >> N;
  factroial = vector<long>(N + 1);
  finverse = vector<long>(N + 1);
  mother = vector<long>(N + 1);

  factroial[0] = 1;
  finverse[0] = 1;
  for (int i = 1; i < N + 1; i++) {
    factroial[i] = (factroial[i - 1] * i) % waru;
    finverse[i] = getpower(factroial[i], waru - 2, waru);
  }
  map<long, vector<long>> path;
  vector<long> treesize(N + 1), rootQA(N + 1);
  for (int i = 0; i < N - 1; i++) {
    long a, b;
    cin >> a >> b;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  counttree(path, 1, -1, treesize);
  rootans(path, 1, -1, rootQA, treesize);
  answer = vector<long>(N + 1);
  answer[1] = rootQA[1];

  for (long x : path[1]) {
    if (1 != x) {
      calans(path, x, 1, treesize, N);
    }
  }
  for (int i = 1; i < N + 1; i++) {
    cout << answer[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
long waru = (long)1e9 + 7;
vector<long> factroial, finverse, mother, answer;
int c = 0;
long getpower(long a, long p, long base) {
  long result = 1; // d = (long)log2(p);
  vector<long> L;
  for (long i = 1; i < p * 2; i *= 2) {
    long ap = ((p / i) % 2 == 1) ? a : 1;
    result = result * ap % base;
    a = (a * a) % base;
  }
  return result;
}
long counttree(map<long, vector<long>> path, long pos, long parent,
               vector<long> &d) {
  long result;
  result = 1;
  for (long x : path[pos]) {
    if (parent != x) {
      result += counttree(path, x, pos, d);
    }
  }
  mother[pos] = parent;
  d[pos] = result;
  return result;
}
long rootans(map<long, vector<long>> path, long pos, long parent,
             vector<long> &d, vector<long> &ts) {
  long result;
  result = factroial[ts[pos] - 1];
  for (long x : path[pos]) {
    if (parent != x) {
      result = result * rootans(path, x, pos, d, ts) % waru;
      result = result * finverse[ts[x]] % waru;
    }
  }
  d[pos] = result;
  return result;
}
void calans(map<long, vector<long>> path, long pos, long parent,
            vector<long> &ts, long N) {
  long result;
  long ans = factroial[N - 1];
  // rootQA[i]*(rootQA[mother](n-1-size)!size!/n!/rootQA[i])<除いた親
  //*(N-1)!/(size-1)!/(N-)!
  ans = ans * answer[parent] % waru;
  ans = ans * finverse[N - 1] % waru;
  ans = ans * factroial[N - 1 - ts[pos]] % waru;
  ans = ans * factroial[ts[pos]] % waru;
  ans = ans * finverse[ts[pos] - 1] % waru;
  ans = ans * finverse[N - 1 - (ts[pos] - 1)] % waru;
  answer[pos] = ans;
  for (long x : path[pos]) {
    if (parent != x) {
      calans(path, x, pos, ts, N);
    }
  }
}
int main() {
  long N;
  cin >> N;
  factroial = vector<long>(N + 1);
  finverse = vector<long>(N + 1);
  mother = vector<long>(N + 1);

  factroial[0] = 1;
  finverse[0] = 1;
  for (int i = 1; i < N + 1; i++) {
    factroial[i] = (factroial[i - 1] * i) % waru;
    finverse[i] = getpower(factroial[i], waru - 2, waru);
  }
  map<long, vector<long>> path;
  vector<long> treesize(N + 1), rootQA(N + 1);
  for (int i = 0; i < N - 1; i++) {
    long a, b;
    cin >> a >> b;
    path[a].push_back(b);
    path[b].push_back(a);
  }
  counttree(path, 1, -1, treesize);
  rootans(path, 1, -1, rootQA, treesize);
  answer = vector<long>(N + 1);
  answer[1] = rootQA[1];

  for (long x : path[1]) {
    if (1 != x) {
      calans(path, x, 1, treesize, N);
    }
  }
  for (int i = 1; i < N + 1; i++) {
    cout << answer[i] << endl;
  }
  return 0;
}