#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int T;
  cin >> T;
  long long int d[100][100];
  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 25; j++) d[i][j] = 0;
  for (int i = 0; i < 100; i++) d[i][0] = 1;
  for (int i = 0; i < 100; i++)
    for (int j = 1; j <= i; j++) d[i][j] = d[i - 1][j] + d[i - 1][j - 1];
  while (T--) {
    long long int N;
    cin >> N;
    long long int n;
    long long int b[30] = {0};
    for (n = 2; N > 0; n++) {
      b[N % n]++;
      N = N / n;
    }
    long long int s1 = 1, s2 = 1, diff = 0;
    long long int sum = 0;
    n--;
    for (int i = n - 1; i; i--) {
      s1 = s1 * d[n - i - sum][b[i]];
      sum = sum + b[i];
    }
    sum = 0;
    for (int i = n - 1; i > 0; i--) {
      s2 *= d[n - i - 1 - sum][b[i]];
      sum += b[i];
    }
    cout << s1 - s2 - 1 << endl;
  }
}
