Using wall-crossing formulae and the theory of mock modular forms we derive a holomorphic anomaly equation for the modified elliptic genus of two M5-branes wrapping a rigid divisor inside a Calabi-Yau manifold. The anomaly originates from restoring modularity of an indefinite theta-function capturing the wall-crossing of BPS invariants associated to D4-D2-D0 brane systems. We show the compatibility of this equation with anomaly equations previously observed in the context of N = 4 topological Yang-Mills theory on È 2 and E-strings obtained from wrapping M5-branes on a del Pezzo surface. The non-holomorphic part is related to the contribution originating from bound-states of singly wrapped M5-branes on the divisor. We show in examples that the information provided by the anomaly is enough to compute the BPS degeneracies for certain charges. We further speculate on a natural extension of the anomaly to higher D4-brane charge.shown in ref. [18]. 3 A similar story showed up in N = 4 topological U (2) SYM theory on È 2 [5], where it was shown that different sectors of the partition function need a nonholomorphic completion which was found earlier in ref. [22] in order to restore S-dualtiy invariance. An anomaly equation describing this non-holomorphicity was expected [5] in the cases where b + 2 (P ) = 1. In these cases holomorphic deformations of the canonical bundle are absent. The non-holomorphic contributions were associated with reducible connections U (n) → U (m) × U (n − m) [5,4]. In ref.[4] this anomaly was furthermore related to an anomaly appearing in the context of E-strings [23]. These strings arise from an M5-brane wrapping a del Pezzo surface B 9 , also called 1 2 K3. The anomaly in this context was related to the fact that n of these strings can form bound-states of m and (n − m) strings. Furthermore, the anomaly could also be related to the one appearing in topological string theory.The anomaly thus follows from the formation of bound-states. Although the holomorphic expansion would not know about the contribution from bound-states, the restoration of duality symmetry forces one to take these contributions into account. The nonholomorphicity can be understood physically as the result of a regularization procedure. The path integral produces objects like theta-functions associated to indefinite quadratic forms which need to be regularized to avoid divergences. This regularization breaks the modular symmetry, restoring the symmetry gives non-holomorphic objects. The general mathematical framework to describe these non-holomorphic completions is the theory of mock modular forms developed by Zwegers in ref. [24]. 4 A mock modular form h(τ ) of weight k is a holomorphic function which becomes modular after the addition of a function g * (τ ), at the cost of losing its holomorphicity. Here, g * (τ ) is constructed from a modular form g(τ ) of weight 2 − k, which is referred to as shadow.Another manifestation of the background dependence of the holomorphic expansions of the topological theories are wall-cross...