The Portuguese recently lived through 50 days of political drama, when the party that lost an October general election outmaneuvered the winner and took charge of the country.

With a Sunday ballot likely heralding the end of two-party politics in neighboring Spain, possible post-election permutations also make the future of the European Union's fifth-largest economy hard to predict. Spain, too, could end up with a so-called "coalition of losers" in power.

"It's never happened in Spain before, but there again, it had never happened in Portugal before either," says Carlos Barrera, a political analyst at Spain's University of Navarra.

Here is how forming a government works in Iberia.

____

A PORTUGUESE SURPRISE

On the night of Oct. 4, Portugal buzzed with speculation about whether Socialist leader Antonio Costa would resign after his party lost that day's general election.

The next month, he was sworn in as prime minister.

Costa's Socialists got about 32 percent of the vote but he outflanked the incumbent center-right government, which won with 38 percent, by creating an unprecedented alliance of leftist parties whose common denominator was a commitment to dismantling austerity measures. Those parties pooled their votes in Parliament to create a majority and unseat the government.

That thrust Portugal's president into the spotlight. Though the head of state has no executive power, the president has the right to choose — taking into account election results — which party to install in government, though the nominated party needs to win a vote of confidence in Parliament before starting work.

President Anibal Cavaco Silva, mindful that debt-heavy Portugal is still recovering from its 78 billion-euro ($85 billion) bailout in 2011, balked at the anti-austerity alliance. But his only other option was to install a caretaker government that nobody wanted.

____

SUSPENSE IN SPAIN

A spectacular surge in support over the past year for two new parties challenging the traditional heavyweights has made this election one of Spain's closest ever.

That could spell an unpleasant surprise for the winner as, just like in Portugal, a narrow win would likely leave the triumphant party at the mercy of a coalition of its defeated rivals.

Recent opinion polls have placed the conservative Popular Party and the main opposition Socialist Party roughly level with the centrist newcomer Ciudadanos party. The radical leftist Podemos party is lagging behind but could turn into a powerbroker.

The expectation is that no single party will collect enough votes to obtain an overall majority of 176 lawmakers in the 350-seat Parliament. That will likely bring a tense period of political horse-trading as parties negotiate to create a government that has enough parliamentary votes to enact its policies without concessions to rivals.

"Government formation is going to be tricky, as in the case of Portugal," said Antonio Barroso, a London-based analyst with the Teneo Intelligence political risk consultancy. "It's clear that no party's going to have an absolute majority so the key question is, what can you have?"

___

KING FELIPE'S ROLE

After the election King Felipe VI will become the focus.

The monarch holds talks with the leaders of each party that has won seats in Parliament. Then, article 99 of the Constitution requires him to nominate one of them for government.

But the nominated party leader must then win a vote of confidence in Parliament in order to take office.

In a first vote, the candidate must get more than 50 percent of the full 350 votes in order to form a government. If he falls short, he must get more votes for him than against him in a second ballot 48 hours later. That's a lower bar which allows parties to abstain, letting a rival into power in return for concessions. If there is still deadlock after two months, the monarch calls a new election.

Historically, the king has simply nominated the election winner as prime minister. But, as Barrera of Navarra University points out, "that is not written down anywhere. It's just a custom."

The king has to consider which party has a real possibility of winning the parliamentary confidence vote and forming a stable government. Felipe will find that out during his talks with the parties. It would not be illegal or unconstitutional for an alliance of the second- and third-placed parties to get the nod, Barrera said. That has often happened in Spanish regional and local elections, but never in a general election, which in Spain has never produced a coalition government.

___

POTENTIAL SPAIN ELECTION OUTCOMES

As opinion polls stand, various possibilities — and impossibilities — stand out.

Business-friendly Ciudadanos could cut a deal with the Popular Party — but one of Ciudadanos leader Albert Rivera's conditions is that incumbent Prime Minister Mariano Rajoy does not stay on for a second term.

The Socialists are unwilling to consider a grand coalition with the Popular Party, but they could also join forces with Ciudadanos, though Rivera doesn't want to join a coalition in which he is the junior member.

The Socialists might ally with Podemos, but the radical party has ruled out a coalition with the Socialists if the Socialists get more votes. Ciudadanos and Podemos are ideologically opposed and are highly unlikely to join forces with the Socialists.