def _mask_first_axes(x, mask, c_contig=None):
    """Fast application of the mask for both C or Fortran-ordered x.
    """
    if c_contig is None:
        c_contig = x.flags.c_contiguous
    if c_contig:
        y = x[mask]
    else:
        shape = (mask.size,) + x.shape[mask.ndim:]
        y = x.reshape(shape, order='F')[mask.ravel('F')]
    return y, c_contig
