Looking back on 2007, it seems amazing that no-one seriously expected the SNP to win the May Scottish election, least of all the Nationalists themselves. But the return of the SNP as the largest party in May rocked Holyrood and Westminster. The new prime minister, Gordon Brown, reacted with uncharacteristic panic and the UK press demanded to know why no-one had seen it coming. But the signs were there, if anyone had cared to look.

Labour MSPs had been hoisting storm warnings, insisting that there was a groundswell of anti-Labour feeling spreading through the constituencies. But most commentators thought that was just the usual exercise in lowering expectations. The truth was that the SNP itself didn't appear to believe that it was on the eve of a historic breakthrough. Indeed, many MSPs thought that the opinion polls were exaggerated and fully expected most of the Labour vote to return to fold.

But throughout 2006, opinion polls had indicated that there was something stirring in the undergrowth of Scottish politics. The Dunfermline and West Fife by-election, where Labour lost an 11,000 majority to the Liberal Democrats in the future prime minister Gordon Brown's home constituency, should have been warning enough. The mood was increasingly "anyone but Labour".

But what no-one expected was that Brown's entry to Number 10 would actually make Labour's situation worse in Scotland. Dramatically worse. The reaction in the UK press to Tony Blair's resignation early in 2007 was hardly unexpected, but the virulence with which sections of the London media attacked Brown for his national origins sickened many Scots.

Labour MPs in Westminster were initially relieved that there was no leadership election following Blair's decision to go, which was apparently taken during his controversial stay in Bee Gee Robin Gibb's mansion in Florida. Time to go. It had become clear to the PM that there was no-one in the Labour Cabinet prepared to challenge Brown for the leadership, apart from the home secretary, John Reid, who had zero chance of success. That the education secretary, Alan Johnson, was the only possibility merely underlined the paucity of talent.

Exploiting a New Year lull in the fighting in Iraq following the execution of Saddam Hussein and the breakthrough in Northern Ireland over devolution, the PM saw his chance and took it. He announced that Gordon Brown would be taking over the helm of government while he tidied his desk in time for his formal departure in May. This gave the chancellor his "hundred days" in which to set out his stall before entering Number 10. But it also gave the press a chance to discredit Brown before he took office.

Editorials raged that this was "a constitutional travesty". "Where else," thundered The Times, "but in the most squalid dictatorship would the leader of the nation be chosen by a back-room cabal? This is a negation of democracy."

The Daily Telegraph went further. "The fact that the future prime minister sits for a Scottish constituency, over which English MPs exercise no influence, makes Gordon Brown unfit to lead any legitimate government in England." The Sun took its own characteristic slant. "Send him home! England will not be ruled by the Scots." Opinion polls in April suggested most English voters agreed.

So did many Labour MPs. Former welfare minister Frank Field led a deputation of 100 Labour MPs and candidates to Tony Blair urging him not to leave office until there was a resolution to the West Lothian Question. "It is simply untenable for the prime minister to hand over, without any democratic election, the leadership of the country to a politician who, for all his personal qualities, is profoundly unacceptable to English opinion."

Brown launched a high-profile tour of England, addressing countless town hall meetings, telling Middle England how much he loved the flag and how his favourite sporting moment was that goal by Gazza against Scotland. His announcement in The Daily Mail that he felt "more English than Scottish" appalled many Scottish voters.

But when he promised to review the Barnett Formula and curb the voting rights of Scottish MPs in Westminster, the balloon really went up. The truth was that very few people in England really understood the West Lothian Question, still less the obscure formula under which increases in Scottish public spending are calculated.

But the UK press was suddenly filled with stories about the iniquities of a system which "robbed England to pay Scotland". About how Scottish Labour MPs held a "veto over English laws" by virtue of their voting rights in Westminster on issues which are devolved to Scotland. Blogs went red hot, with denunciations of the "Scottish Raj" and the "greedy jocks" who were bleeding England white.

This was Alex Salmond's moment, and he seized it. Realising that the outpouring of English resentment was a unique opportunity for the SNP, the Nationalist leader started his own tour of English towns, arguing that a new constitutional settlement was indeed essential for Britain. Many commentators in Scotland thought he had completely lost his marbles. Why wasn't the leader of the SNP back in Scotland, campaigning for his party in the Scottish elections, instead of traipsing round England, persuading people who have no vote?

But Salmond was shrewdly exploiting the increasing anglicisation of the Scottish media. Much of the Scottish press is now dominated by Scottish editions of London papers and Salmond had calculated that he would gain better projection by campaigning in England than in Scotland. The London media became obsessed with this extraordinary double act Brown and Salmond - "the two Scots fighting for the soul of England". The Murdoch Press gave Salmond acres of coverage, as the "only Scot who will tell it like it is - that Scots have too much say in England".

Salmond's line was that Scotland was indeed over-dependent on England, and should have no say in English affairs in Westminster. The SNP had for many years voluntarily abstained from voting on English legislation in Westminster. Salmond didn't accept that England subsidised Scotland, but he argued that the present arrangements, under which Scotland gets around 15% more per head in public spending than the UK average, was "discredited and out of date". Salmond called instead for the Scottish parliament to be given powers over taxation so that "Scotland will only spend what she earns".

The UK press took up this fiscal autonomy and ran with it. An issue which had been of marginal interest in the Scottish media for years, suddenly became the dominant issue in the UK, leading the BBC's Six O'clock News day after day. Gordon Brown argued in vain that, in reality, the Barnett Formula had actually narrowed the gap in relative spending between Scotland and England since Labour came to office. He was hoisted by his own fiscal petard.

Similarly, in agreeing to restrict the voting rights of Scottish MPs in Westminster, the prime minister played into SNP hands. The sight of Scots being denied the right to vote on the new generation of nuclear power stations, for example, because the first ones were planned for England, incensed many Scots.

The Conservative leader, David Cameron, took full advantage of the situation to argue that Labour no longer had a mandate to govern in England. He based this on the fact that the Conservatives had won a majority of votes in England at the last general election. Conservative ministers, he argued, should be given joint responsibility in devolved ministries, such as health and education, on the grounds that the government no longer had legitimacy on devolved areas. This was pure fantasy, of course, but it played very well in the English right-wing press, who saw an opportunity to use the Scottish Question to drive Labour from office.

The Scottish elections fell at the height of this extraordinary ferment in UK constitutional politics. The election campaign turned into a referendum on the Union rather than the election of a devolved government in Holyrood. The timing could not have been more apposite, coming on the 300th anniversary of the 1707 Treaty of Union.

UK Labour ministers came north again to warn of the dangers of Scotland going it alone. "Divorce," they insisted, "is an expensive business," as they urged Scots not to endanger the "Union dividend". But against the clamour of the English media demanding Scotland be punished for fiscal indolence, and interfering in English law-making, they had a difficult job arguing that the Union was still a harmonious one. And the Union dividend seemed particularly questionable now that the Barnett system was being scrapped by the new prime minister.

Even so, it was a profound shock when the SNP emerged with a slim majority of four seats over Labour in May's Holyrood elections. A combination of the Iraq war, the lacklustre performance of the Labour-led Scottish Executive, and an increasing frustration with the Union conspired to break Labour's hold on Scotland. Widespread abstentionism by Labour voters and tactical voting to the Liberal Democrats did the rest.

Sections of the UK press pronounced that the Scots had voted to go it alone, and demanded that Scottish MPs should retreat from Westminster voluntarily. "At last, England will be free again," crowed The Sun. Alex Salmond - perhaps carried away by the London press dubbing him the "New Wallace" - announced he intended to form a "provisional government" for an independent Scotland and announced that his first act as first minister would be a bill to hold a referendum on independence.

But everyone had seemingly forgotten the electoral system. The SNP didn't have an outright majority. The Liberal Democrats, now the third party in Scotland with only eight fewer seats than Labour, refused to join with the SNP unless it dropped its commitment to holding a referendum on independence. The 10 Green MSPs said that the SNP couldn't be trusted on the environment.

Labour, under its new leader, Tom McCabe, invited the Scottish LibDems and the Greens to help form a minority administration, to "save the UK". Talks began as the Nationalists complained that they were being locked out of power by a "unionist cabal" and that, as the biggest party, the SNP had a "moral claim" to lead any Holyrood administration. But there is nothing in the Holyrood rules that says the largest party has to form a government.

In the end, the "unionist alliance" collapsed as the Greens refused to abandon their commitment to independence, and Labour refused LibDem demands for their leader, Nicol Stephen, to be made first minister. For weeks, Scotland was without government, and there began to be serious questions about the stability of the Scottish Executive. Labour ministers remained in power in a caretaker capacity, but without any constitutional authority so to do. Then Gordon Brown had his "moment of madness".

No-one quite knows why the prime minister decided to call a referendum on the constitution. There was certainly no demand for one in Scotland. It seems that Brown felt that the only way to resolve the matter would be to pre-empt the Nationalists and hold a referendum on independence, but not at a time of Alex Salmond's choosing.

With the Scottish coalition in disarray, Brown hoped that Scots would see the folly of following the Nationalists and deliver a crushing rejection of independence. But the bill in Westminster created a furious reaction in Scotland, where people argued that the prime minister was treating Scots with contempt by holding an "opportunistic plebiscite" during a period of electoral instability. The move pushed the LibDems into a coalition with the SNP, and created a constitutional confrontation between Holyrood and Westminster.

As this extraordinary year drew to a close, the SNP and the LibDems put together a programme for an essentially federalist settlement under which Scotland would get substantial tax raising powers; a greater share of oil revenues; and powers over issues such as immigration, nuclear power and broadcasting. At present, opinion polls suggest that this is the option which will win most support in the forthcoming referendum. But in truth, no-one really knows what will happen in 2008.

The union survived its 300th birthday, but only just.