#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ll = long long;
using pii = pair<int, int>;
const int N = 2123456;
const int INF = int(1e9);
LL x;
LL C[25][25];
int main() {
  for (int i = 0; i <= 20; ++i) {
    C[i][0] = 1;
    for (int j = 1; j <= i; ++j) C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  }
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    cin >> x;
    vector<int> cnt(25);
    int n = 0;
    for (int i = 2;; ++i) {
      if (x == 0) break;
      ++n;
      ++cnt[x % i];
      x /= i;
    }
    LL ans = 1;
    int alr = 0;
    for (int i = n; i >= 0; --i) {
      if (cnt[i] == 0) continue;
      ans *= C[min(n - i + 1, n) - alr][cnt[i]];
      alr += cnt[i];
    }
    if (cnt[0]) {
      LL tot = 1;
      alr = 1;
      for (int i = n; i >= 1; --i) {
        if (cnt[i] == 0) continue;
        int cur = min(n - i + 1, n) - alr;
        if (cur < cnt[i]) {
          tot = 0;
          break;
        }
        tot *= C[cur][cnt[i]];
        alr += cnt[i];
      }
      ans -= tot;
    }
    cout << ans - 1 << '\n';
  }
  return 0;
}
