  A search is presented for a right-handed W boson (W$_\mathrm{R}$) and a heavy
neutrino (N), in a final state consisting of two same-flavor leptons (ee or
$\mu\mu$) and two quarks. The search is performed with the CMS experiment at
the CERN LHC using a data sample of proton-proton collisions at a
center-of-mass energy of 13 TeV corresponding to an integrated luminosity of
138 fb$^{-1}$. The search covers two regions of phase space, one where the
decay products of the heavy neutrino are merged into a single large-area jet,
and one where the decay products are well separated. The expected signal is
characterized by an excess in the invariant mass distribution of the
final-state objects. No significant excess over the standard model background
expectations is observed. The observations are interpreted as upper limits on
the product of W$_\mathrm{R}$ production cross sections and branching fractions
assuming that couplings are identical to those of the standard model W boson.
For N masses $m_\mathrm{N}$ equal to half the W$_\mathrm{R}$ mass
$m_\mathrm{W_R}$ ($m_\mathrm{N}$ = 0.2 TeV), $m_\mathrm{W_R}$ is excluded at
95% confidence level up to 4.7 (4.8) and 5.0 (5.4) TeV for the electron and
muon channels, respectively. This analysis provides the most stringent limits
on the W$_\mathrm{R}$ mass to date.
