#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
ll pw(ll a, ll n) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1 == 1) {
      ret = (ret * a) % MOD;
    }
    a = (a * a) % MOD;
    n = n >> 1;
  }
  return ret;
}
ll inv(ll a) { return pw(a, MOD - 2); }
void solve() {
  ll D;
  cin >> D;
  vl primes;
  for (ll i = 2; i * i <= D; i++) {
    if (D % i == 0) {
      primes.push_back(i);
      while (D % i == 0) {
        D /= i;
      }
    }
  }
  if (D > 1) {
    primes.push_back(D);
  }
  vl fat(101);
  fat[0] = 1;
  fat[1] = 1;
  vl invfat(101);
  invfat[0] = 1;
  invfat[1] = 1;
  for (int i = 2; i <= 100; i++) {
    fat[i] = (fat[i - 1] * i) % MOD;
    invfat[i] = (invfat[i - 1] * inv(i)) % MOD;
  }
  int sz = primes.size();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    vl difs_u;
    vl difs_d;
    ll sum1 = 0;
    ll sum2 = 0;
    ll ans = 1;
    for (int i = 0; i < sz; i++) {
      ll x1 = 0;
      ll x2 = 0;
      while (u % primes[i] == 0) {
        x1++;
        u /= primes[i];
      }
      while (v % primes[i] == 0) {
        x2++;
        v /= primes[i];
      }
      ll dif = x1 - x2;
      if (dif == 0) continue;
      if (dif < 0) {
        dif = -dif;
        sum2 += dif;
      } else {
        sum1 += dif;
      }
      ans = (ans * invfat[dif]) % MOD;
    }
    ans = (ans * fat[sum1]) % MOD;
    ans = (ans * fat[sum2]) % MOD;
    cout << ans << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
