  For any Shimura variety of Hodge type with hyperspecial level at a prime $p$
and a lisse sheaf on it, we prove a formula, conjectured by Kottwitz
\cite{Kottwitz90}, for the Lefschetz number of an arbitrary Frobenius-twisted
Hecke correspondence acting on the compactly supported \'etale cohomology and
verify another conjecture of Kottwitz \cite{Kottwitz90} on the stabilization of
that formula. The main ingredients of our proof of the formula are a recent
work of Kisin \cite{Kisin17} on Langlands-Rapoport conjecture and the theory of
Galois gerbs developed by Langlands and Rapoport \cite{LR87}. Especially, we
use the Galois gerb theory to establish an effectivity criterion of Kottwitz
triple, and mimic the arguments of Langlands and Rapoport of deriving the
Kottwitz formula from their conjectural description of the $\Fpb$-point set of
Shimura variety (Langlands-Rapoport conjecture). We do not assume that the
derived group is simply connected, and also obtain partial results at (special)
parahoric levels under some condition at $p$. For that, in the first part of
our work we extend the results of Langlands and Rapoport to such general cases.
