code: def _nthroot_mod1(s, q, p, all_roots):
      """
      Root of ``x**q = s mod p``, ``p`` prime and ``q`` divides ``p - 1``.
      Assume that the root exists.
  
      Parameters
      ==========
  
      s : integer
      q : integer, n > 2. ``q`` divides ``p - 1``.
      p : prime number
      all_roots : if False returns the smallest root, else the list of roots
  
      Returns
      =======
  
      list[int] | int :
          Root of ``x**q = s mod p``. If ``all_roots == True``,
          returned ascending list. otherwise, returned an int.
  
      Examples
      ========
  
      >>> from sympy.ntheory.residue_ntheory import _nthroot_mod1
      >>> _nthroot_mod1(5, 3, 13, False)
      7
      >>> _nthroot_mod1(13, 4, 17, True)
      [3, 5, 12, 14]
  
      References
      ==========
  
      .. [1] A. M. Johnston, A Generalized qth Root Algorithm,
             ACM-SIAM Symposium on Discrete Algorithms (1999), pp. 929-930
  
      """
      g = next(_primitive_root_prime_iter(p))
      r = s
      for qx, ex in factorint(q).items():
          f = (p - 1) // qx**ex
          while f % qx == 0:
              f //= qx
          z = f * invert(-f, qx)
          x = (1 + z) // qx
          t = discrete_log(p, pow(r, f, p), pow(g, f * qx, p))
          for _ in range(ex):
              # assert t == discrete_log(p, pow(r, f, p), pow(g, f*qx, p))
              r = pow(r, x, p) * pow(g, -z * t % (p - 1), p) % p
              t //= qx
      res = [r]
      h = pow(g, (p - 1) // q, p)
      # assert pow(h, q, p) == 1
      hx = r
      for _ in range(q - 1):
          hx = (hx * h) % p
          res.append(hx)
      if all_roots:
          res.sort()
          return res
      return min(res)
  
imports: from __future__ import annotations
  from sympy.external.gmpy import gcd, lcm, invert, sqrt, legendre, jacobi, kronecker, bit_scan1, remove
  from sympy.polys import Poly
  from sympy.polys.domains import ZZ
  from sympy.polys.galoistools import gf_crt1, gf_crt2, linear_congruence, gf_csolve
  from .primetest import isprime
  from .factor_ import factorint, _perfect_power
  from .modular import crt
  from sympy.utilities.decorator import deprecated
  from sympy.utilities.memoization import recurrence_memo
  from sympy.utilities.misc import as_int
  from sympy.utilities.iterables import iproduct
  from sympy.core.random import _randint, randint
  from itertools import product
label_desc: Use image node_id 15 for calling a global function with example usage: _nthroot_mod1(s, q, p, all_roots) and returns: min, res