Modeling sequences and temporal networks with dynamic community structures