  We study an invariant, the secondary trace, attached to two commuting
endomorphisms of a 2-dualizable object in a symmetric monoidal higher category.
We establish a secondary trace formula which encodes the natural symmetries of
this invariant, identifying different realizations as an iterated trace. The
proof consists of elementary Morse-theoretic arguments (with many accompanying
pictures included) and may be seen as a concrete realization of the cobordism
hypothesis with singularities on a marked 2-torus. From this perspective, our
main result identifies the secondary trace with two alternative presentations
coming from the standard generators S and T of the mapping class group SL_2(Z).
  We include two immediate consequences of the established invariance. The
first is a modular invariance property for the 2-class function on a group
arising as the 2-character of a categorical representation. The second is a
generalization (for coherent sheaves or D-modules) of the Atiyah-Bott-Lefschetz
formula conjectured by Frenkel-Ng\^o in the case of a self-map of a smooth and
proper stack over a general base.
