Presidential elections will be held in Ecuador on Sunday at a tumultuous moment for the country. President Guillermo Lasso called snap elections in May amid impeachment proceedings against him over accusations of embezzlement. This month, the presidential candidate Fernando Villavicencio was assassinated on the campaign trail.

All this has unfolded as foreign drug mafias have joined forces with local prison and street gangs to unleash a wave of violence unlike anything in the country’s recent history, sending homicide rates to record levels and making security the leading issue for most voters.

Here’s what you need to know about the upcoming vote.

Mr. Lasso disbanded the country’s opposition-led legislature in May, using, for the first time, a constitutional measure that allows the president to rule by decree until new presidential and congressional elections can be held. The impeachment proceedings were permanently halted once Mr. Lasso dissolved congress.

The move came amid a moment of extraordinary political turbulence for Ecuador, a country of 18 million on South America’s western edge. But it provided temporary stability by allowing the president to bypass the deadlocked legislature and appease voters hungry for new leadership and action against the rise in street crime and drug and gang violence.

Candidates can win outright by taking 50 percent of the total vote or 40 percent along with a 10 percentage point lead over the runner-up. Failing that, the top two candidates will compete in a runoff election on Oct. 15.

The new president will hold office until May 2025.

The votes will be cast and counted using blockchain technology to avoid voter fraud, according to the Ecuadorean electoral council, a first in Latin America.

The campaign for Sunday’s elections was convulsed on Aug. 9, when Mr. Villavicencio was fatally shot at a campaign event. Six Colombians have been arrested in connection with the brazen killing, but it remains unclear who, if anyone, hired them.

Mr. Villavicencio was a legislator, former investigative journalist and anti-corruption activist. While he was not a top contender, polling near the middle of an eight-person race, he had a long history in Ecuadorean public affairs, largely as an antagonist to those in power.

He played a crucial role in exposing a bribery scandal that eventually led to the conviction of a former president, Rafael Correa. Some of his work led to death threats.

He had been outspoken about the link between organized crime and the political establishment, which earned him enemies. The attack in broad daylight was a traumatizing event for an election that has been dominated by concerns over drug-related violence.

The candidate leading in the polls is Luisa González, backed by the powerful party of the former president, Mr. Correa, who governed from 2007 to 2017. During his presidency, a commodities boom helped lift millions out of poverty, but Mr. Correa’s authoritarian style and accusations of corruption deeply divided the country.

“We’re seeing a lot of voter nostalgia for the security situation and the economic situation while he was in power, which seems to be propelling her candidacy,” said Risa Grais-Targow, the Latin America director for Eurasia Group. “The rest of the field is in a really tight battle for second place.”

That would include Otto Sonnenholzner, a former vice president, and an Indigenous activist, Yaku Pérez, who has been campaigning on environmental issues.

“Otto is trying to position himself as a more kind of centrist newcomer,” said Ms. Grais-Targow, but to many voters he represents “policy continuity from Lasso.”

As for Mr. Pérez, his focus on the environment and corruptions are not the main voter concerns, she said.

Christian Zurita, Mr. Villavicencio’s longtime investigative partner and close friend, has replaced him as his party’s pick, but he is regarded as a long shot.

While security was always going to be a top issue, now “this election will be largely about the issue of safety,” said Paolo Moncagatta, a political analyst based in Quito, the capital.

Experts predict that this could elevate the fortunes of a previously obscure candidate, Jan Topic, a 40-year-old businessman and former soldier in the French Foreign Legion who is emphasizing a tough stance on crime.

He has echoed the promises of El Salvador’s president, Nayib Bukele, whose hard-line approach to gangs has significantly reduced violence rates, though his aggressive tactics have raised concerns from human rights watchdogs.

Polls in Ecuador tend to be unreliable, but the latest numbers suggest that Ms. González’s lead is shrinking, and a recent surge by Mr. Topic has him neck and neck with Mr. Sonnenholzner for second place.

Germán Martínez, a coroner who works at the morgue where Mr. Villavicencio’s body lay last week, said that after the killing he had decided to switch his vote to Mr. Topic.

“This can’t keep happening here in the country,’’ he said. “We are looking for someone who will confront all this with an iron fist.”

Many of Mr. Villavicencio’s supporters blame his killing on his political enemy, Mr. Correa. There is no evidence that Mr. Correa or his party, Citizen Revolution Movement, was involved in the assassination, but experts say the fallout could nevertheless hurt Ms. González in the elections.

Analysts caution that rather than driving voters to the polls, increased safety concerns could just as easily persuade them to stay home, despite a mandatory voting law that imposes fines for absenteeism.

“Voting is scary,” said Ana Vera, 44, a housekeeper in Quito.

Worries over security deepened in the past week when shootings were reported near appearances by candidates. In one case on Thursday, a shooting occurred in Durán, near where Daniel Noboa, a presidential candidate, was holding an event. The authorities said he was not a target.

And on Saturday a shooting occurred outside a restaurant in Guayaquil, where Mr. Sonnenholzner was eating, though, the authorities said that in this case, too, he was not a target.

Ecuador was once a tranquil haven compared with its neighbor Colombia, for decades torn by violence among armed guerrilla and paramilitary groups and drug cartels. That all changed in the past few years as Colombia forged a peace deal, and Ecuador became dominated by an increasingly powerful narco-trafficking industry.

Amid news reports regularly featuring beheadings, car bombs, police assassinations, young men hanging from bridges and children gunned down outside their homes or schools, Ecuadoreans are hoping for new leadership that can restore the peaceful existence they once took for granted.

Jenny Goya, 29, was in taxicab in downtown Guayaquil, the country’s largest city, recently when the driver suddenly took a detour. Two armed men got into the vehicle, stole her belongings and emptied her bank accounts. After holding her for two hours, they left her on the street.

“I had always felt quite safe on the street despite the crime, but now I avoid going out as much as possible,” said Ms. Goya, a university administrator. “I also started to feel unsafe in enclosed spaces.”

“I started to feel that no space was safe,” she added.

Thalíe Ponce contributed reporting.