Formulas for calculating the extremal ranks and inertias of a matrix-valued function subject to matrix equation restrictions