#include <bits/stdc++.h>
using namespace std;
long long C(int x, int y) {
  if (x < y) return 0;
  long long ans = 1;
  for (int i = 1; i <= y; i++) ans *= (x - i + 1), ans /= i;
  return ans;
}
int T, cnt[25], tot;
long long n, ans1, ans2;
int main() {
  scanf("%d", &T);
  while (T--) {
    ans1 = 1;
    ans2 = 0;
    tot = 0;
    scanf("%lld", &n);
    memset(cnt, 0, sizeof(int[20]));
    for (int i = 2; n; i++) cnt[n % i]++, n /= i, tot++;
    int tmp = tot;
    for (int i = tot + 1; i >= 1; i--)
      ans1 *= C(tmp - i + 1, cnt[i]), tmp -= cnt[i];
    if (cnt[0]) {
      ans2 = 1;
      tot--;
      tmp = tot;
      for (int i = tot + 1; i >= 1; i--)
        ans2 *= C(tmp - i + 1, cnt[i]), tmp -= cnt[i];
    }
    printf("%lld\n", ans1 - ans2 - 1);
  }
  return 0;
}
