code: def sqrt_mod(a, p, all_roots=False):
      """
      Find a root of ``x**2 = a mod p``.
  
      Parameters
      ==========
  
      a : integer
      p : positive integer
      all_roots : if True the list of roots is returned or None
  
      Notes
      =====
  
      If there is no root it is returned None; else the returned root
      is less or equal to ``p // 2``; in general is not the smallest one.
      It is returned ``p // 2`` only if it is the only root.
  
      Use ``all_roots`` only when it is expected that all the roots fit
      in memory; otherwise use ``sqrt_mod_iter``.
  
      Examples
      ========
  
      >>> from sympy.ntheory import sqrt_mod
      >>> sqrt_mod(11, 43)
      21
      >>> sqrt_mod(17, 32, True)
      [7, 9, 23, 25]
      """
      if all_roots:
          return sorted(sqrt_mod_iter(a, p))
      p = abs(as_int(p))
      halfp = p // 2
      x = None
      for r in sqrt_mod_iter(a, p):
          if r < halfp:
              return r
          elif r > halfp:
              return p - r
          else:
              x = r
      return x
  
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 8 for calling a global function with example usage: sqrt_mod(a, p, all_roots) and returns: x, sorted, r, unknown