When President Donald Trump fired FBI Director James Comey, he triggered the appointment of Robert Mueller as an independent special counsel and caused the FBI to open a formal investigation into the president. This set in motion a dramatic chain of events, which would put the president on a collision course with the Department of Justice.

In his new book, "Deep State: Trump, the FBI, and the Rule of Law," the journalist James B. Stewart draws on scores of interviews with key Justice Department and White House officials and voluminous documents to recount the FBI investigations of both Trump and his 2016 Democratic opponent, Hillary Clinton.

In this excerpt, Stewart tells the story of how an investigation into the scandal-racked Anthony Weiner kicked off a chain of events that would upend the Clinton campaign.

Two days after the FBI received the Steele dossier, on September 21, the London tabloid Daily Mail ran a front-page "exclusive":

"Anthony Weiner carried on a months-long online sexual relationship with a 15-year-old girl during which she claims he asked her to dress up in 'school-girl' outfits for him on a video messaging application and pressed her to engage in 'rape fantasies,' DailyMail.com can exclusively report. "The girl, whose name is being withheld by DailyMail.com because she is a minor, said the online relationship began last January while she was a high school sophomore and before Weiner's wife, Hillary Clinton's aide Huma Abedin, announced she was ending their marriage. "Weiner was aware that the girl was underage, according to DailyMail.com interviews with the girl and her father, as well as a cache of online messages."

In just three paragraphs, the British tabloid had laid out all the elements of a crime defined by New York Penal Law 235: disseminating indecent materials to minors. Because it involved a minor, this incident was exponentially more threatening to Weiner than his prior much-publicized sexual escapades, which had ended his once-promising career in Congress, shattered his 2013 run for New York City mayor, and — after he sent lurid photos while in bed with his 4-year-old son — finally caused Huma Abedin to leave him and file for divorce.

Hillary Clinton and Huma Abedin. Through it all Clinton had loyally stood by her top aide. They were so close that Clinton referred to Abedin as a "second daughter." Bill Clinton officiated at her 2010 marriage to Weiner, then an up-and-coming Democratic congressman. Clinton had firmly rebuffed suggestions to distance herself from Abedin as the Weiner scandals mounted and Abedin loyally and painfully stood by her husband. Abedin was vice-chair of Clinton's campaign and by the candidate's side at a Hamptons fund-raiser when the Daily Mail story broke.

As The New York Times noted, Weiner's behavior "threatens to remind voters about the troubles in the Clintons' own marriage over the decades, including Mrs. Clinton's much-debated decision to remain with then-President Bill Clinton after revelations of his relationship with Monica Lewinsky. Ms. Abedin's choice to separate from her husband evokes the debates that erupted over Mrs. Clinton's handling of the Lewinsky affair, a scandal her campaign wants left in the past."

In what would prove to be a spectacular miscalculation, The Times reported that Clinton's advisers "were confident Mr. Weiner's actions would not hurt Mrs. Clinton."

Anthony Weiner. AP Following the Daily Mail article, the New York field office and the Manhattan US attorney, Preet Bharara, took the lead in the Weiner investigation. On September 26, the government asked for and was granted a search warrant and seized Weiner's iPhone, iPad, and laptop computer the same day. One of the FBI's digital extraction technicians noticed within hours that there were about 340,000 emails on the laptop. Among the domain addresses were Yahoo.com, State.gov, Clintonfoundation.org, Clintonemail.com, and Hillaryclinton.com. "Am I seeing what I think I'm seeing?" the technician wondered.

At the technician's request, the computer was looked at by another agent, who described it as an "oh shit" moment and agreed they needed to report the discovery "up the chain" immediately. They also drafted an email that began, "Just putting this on the record because of the optics of the case."

Andrew McCabe. REUTERS/James Lawler Duggan Two days later, on September 28, the New York FBI office's assistant director, Bill Sweeney, relayed news of the discovery during a weekly teleconference with FBI headquarters in Washington. Ordinarily, then-FBI Director James Comey would have been presiding, but he was testifying that afternoon on Capitol Hill, so Andrew McCabe, Comey's deputy, handled it. One participant said that Sweeney's revelation was like "dropping a bomb in the middle of the meeting" and stated that "everybody realized the significance of this, like, potential trove of information." He said Sweeney "very much emphasized the significance of what he thought they had there."

But McCabe later had only a hazy memory of Sweeney's remarks. Later that day, he told Comey, in passing, "Hey, Boss, I just want you to know that the criminal squad in New York has got Anthony Weiner's laptop and I think it may have some connect to Midyear"

"Hey, Boss, I just want you to know that the criminal squad in New York has got Anthony Weiner's laptop and I think it may have some connect to Midyear"

— the name of the team of Justice Department officials investigating Clinton's emails — or something to that effect, and he might have mentioned Abedin. But McCabe's comments didn't sink in. Comey didn't make the connection that Weiner was married to Abedin or that Clinton's emails had been found on his laptop.

Peter Strzok, the FBI agent in charge of the Clinton email investigation, texted Lisa Page, a bureau attorney, that evening: "Got called up to Andy's earlier ... hundreds of thousands of emails turned over by Weiner's atty to sdny, includes a ton of material from spouse. Sending team up tomorrow to review ... this will never end." Strzok even considered going himself: "So I kinda want to go up to NY tomorrow, coordinate this."

A team was dispatched but didn't get very far. The search warrant used to seize Weiner's laptop covered only child pornography and disseminating indecent materials — not Hillary Clinton's emails. The US attorney's office had told the agents they couldn't open and read the Clinton-Abedin emails without another search warrant, though it was OK to read the headers.

At this juncture, the New York agents thought the Midyear team in Washington was going to ask for guidance about getting a search warrant and get back to them. Strzok and others on the Midyear team were under the impression that agents in the New York office would continue processing the laptop and get back to them with more information about what was on it, a task that could easily take months — in "January, February 2017, whenever it gets done," according to Strzok. Others, too, thought the legal and technical issues involved in gaining access to the emails would take months to resolve, well after the coming election.

Any sense of urgency drained away. While sporadic discussions of the Weiner laptop continued within lower ranks at FBI headquarters, it wasn't even on Comey's radar. Strzok got back to the all-consuming task of the Russia investigation.

McCabe alerted the Justice Department about the Weiner laptop the first week in October and told a Justice Department lawyer he was sending an agent to review the emails. But both thought they would mostly be duplicates of what they'd already seen, given how thorough the investigation had been.

The "Access Hollywood" tape. Getty Images That was as far as it got. A few days later, on October 7, The Washington Post published a video showing Donald Trump on his way to tape a 2005 episode of "Access Hollywood" in which Trump boasted to the host Billy Bush: "I don't even wait. And when you're a star, they let you do it. You can do anything. Grab them by the pussy. You can do anything." In the midst of the resulting furor, WikiLeaks released another batch of thousands of emails hacked from a Gmail account belonging to John Podesta, chair of the Clinton campaign. The Department of Homeland Security and the director of national intelligence issued a joint statement blaming the hack on Russia, noting that "only Russia's senior-most officials could have authorized these activities."

Two days later, FBI agents contacted Podesta, who told reporters on Clinton's campaign plane that he'd spoken with the FBI that weekend. "Russian interference in this election and apparently on behalf of Trump is, I think, of the utmost concern to all Americans, whether you're a Democrat or independent or Republican," Podesta said. And he suggested the Trump campaign might have been in on the leaks, noting that the Trump campaign adviser Roger Stone had boasted about his ties to the WikiLeaks founder, Julian Assange. "So I think it's a reasonable assumption to — or at least a reasonable conclusion — that Mr. Stone had advance warning and the Trump campaign had advance warning about what Assange was going to do," Podesta said. He also cited Trump's perplexing "bromance" with Russian President Vladimir Putin and added that Trump's foreign policy positions "are more consistent with Russian foreign policy than with US foreign policy."

He also cited Trump's perplexing "bromance" with Vladimir Putin and added that Trump's foreign policy positions "are more consistent with Russian foreign policy than with U.S. foreign policy."

Podesta had just publicly revealed, perhaps inadvertently, the closely guarded secret that the FBI was indeed investigating Russian election interference and ties to the Trump campaign.

The Weiner laptop investigation might have languished indefinitely but for the determined efforts of the New York case agent who examined the laptop's contents. (The FBI declined to identify the New York case agent who discovered the Clinton emails on Weiner's laptop and agitated to pursue the investigation.) As the sole proprietor of what he now knew to be hundreds of thousands of emails with Clinton's name on them, and the election just a month away, he was, as he later put it, "a little scared." Even though "I'm not political" and "I don't care who wins this election," he feared the revelation that the bureau sat on such a trove "is going to make us look really, really horrible."

As he put it, "Something was going to come crashing down." Even though "I didn't work the Hillary Clinton matter. My understanding at the time was I am telling you people I have private Hillary Clinton emails, number one, and BlackBerry messages, number two. I'm telling you that we have potentially ten times the volume that Director Comey said we had on the record. Why isn't anybody here?" He also worried that Comey hadn't been informed. "As a big admirer of the guy, and I think he's a straight shooter, I felt like he needed to know that we got this. And I didn't know if he did."

Feeling he "had nowhere else to turn," on October 19 he went outside the normal chain of command and met with two prosecutors from the Manhattan US attorney's office. He figured if they "got the attention of Preet Bharara, maybe they'd kick some of these lazy FBI folks in the butt and get them moving."

Preet Bharara. REUTERS/Eduardo Munoz The prosecutors got the sense that the agent was stressed and worried he'd be blamed if nothing more were done and the existence of the emails became public. He worried that "somebody was not acting appropriately, somebody was trying to bury this." Concerned that the agent might "act out," they briefed Bharara. Although the Clinton email investigation lay outside the Southern District's jurisdiction, Bharara had someone get in touch with George Toscas, the deputy attorney general overseeing the Clinton investigation, in case "something had fallen through the cracks."

The news that his message had gotten through came as a relief to the agent. "Not to sound sappy, but I appreciate you guys understanding how uneasy I felt about the situation," he said in an October 21 email to the Southern District prosecutors he'd met with. And he wrote to his boss and another agent in New York: The prosecutors "understood my concerns yesterday about the nature of the stuff I have on Weiner computer (ie, that I will be scapegoated if it comes out that the FBI had this stuff). They appreciated that I was in a tight spot and spoke to their chain of command who agreed." He now felt reassured "I did the right thing by speaking up."

Two days later, on October 23, the Wall Street Journal reporter Devlin Barrett broke the news that the Virginia governor Terry McAuliffe's political action committee had given $467,500 to the unsuccessful state senate campaign of Jill McCabe, Andrew Mccabe's wife: "Clinton Ally Aided Campaign of FBI Official's Wife."

That came as news to McCabe, who, by design, had known nothing about contributions to his wife's campaign.

The FBI issued a statement, saying McCabe played no role in the campaign and at the time had no involvement in any Clinton investigations. The article also noted that McCabe had sought ethics guidance and had followed it.

Still, that McAuliffe had given such a large sum to Jill McCabe's campaign made Comey uneasy about the appearance of any influence or conflicts. He wished McCabe had told him (unaware that McCabe hadn't known). In that case, he would have assigned someone else to oversee the email investigation — not because he thought there was an actual conflict or that McCabe had done anything improper, but because it might be used "to undercut the credibility of the institution."

Those concerns were immediately borne out: Trump promptly tweeted a link to the article, and the Republican National Committee chair, Reince Priebus, issued a statement: "Given all we know about how the corrupt Clinton machine operates, it's hard not to see this as anything other than a down payment to influence the FBI's criminal investigation into Hillary Clinton's private email server."

Rudy Giuliani jumped on the news, calling the Journal story a "shot to the solar plexus" in an appearance on "Fox & Friends." He added, "We've got a couple of surprises left."

The day after the Wall Street Journal story, Toscas asked McCabe about the status of the Weiner laptop, a subject that had largely slipped McCabe's mind. McCabe asked Strzok and Bill Priestap, the assistant director of the FBI's counterintelligence division, and realized with some dismay that the investigation had languished. As Jim Baker, the FBI's general counsel, put it, "We took our collective eyes off the ball, didn't pay attention to it, and when it came back and we were informed that it was not resolved, then it became a crisis."

Prompted by McCabe's questions, on October 26 Strzok and Toscas convened a conference call that included other Midyear team members, and they finally spoke directly with the New York case agent. He felt frustrated, as he put it, that they were "asking questions that I had already repeatedly answered in other calls." The laptop contained as many as 650,000 emails, and as the agent told them, "We could have every email that Huma and Hillary ever sent each other."

"We could have every email that Huma and Hillary ever sent each other."

These appeared to include the emails from Clinton's earliest days as secretary of state —the time period when she was most likely to have explained or commented on her decision to use private email — which the FBI had been unable to retrieve through other means.

That revelation rekindled the sense of urgency that had briefly prevailed back in September. For Strzok this was the "tipping point" that this might not be just another case of duplicate emails. Strzok briefed Page, who described the development as "good news, in a bad news way" — good news, she explained, in the sense that "more evidence is always good news. It might either change our decision or outcome or further substantiate the outcome we reached." And bad news, because "I cannot believe we are, we are here. We are doing this again on October 26th. Like, oh, my goodness."

That same day, The Wall Street Journal's Barrett again contacted Mike Kortan, then the FBI's assistant director for public affairs, indicating he wasn't finished with the McCabe story. Now he was pursuing a lead that McCabe had tried to tamp down the FBI's Clinton Foundation investigation before the election, which, if true, suggested that McCabe had favored Clinton in the wake of his wife's McAuliffe-financed campaign.

In an email to Kortan, Barrett asked whether it was "accurate" that, "in the summer, McCabe himself gave some instruction as to how to proceed with the Clinton Foundation probe, given that it was the height of election season and the FBI did not want to make a lot of overt moves that could be seen as going after her or drawing attention to the probe." He asked, "Anything else I should know?"

Kortan conferred with Page and McCabe, who suggested Page talk to Barrett and find out more about the story.

That same day, Giuliani again teased a coming "surprise" that would propel Trump to victory. Appearing again on Fox News, he said to expect "a surprise or two that you're going to hear about in the next two days.

"I'm talking about some pretty big surprise," he said.

Comey and McCabe wondered, what was Giuliani talking about? Was someone in the FBI, likely in New York, leaking?

Comey ordered the FBI's inspection division to launch an investigation. "I was concerned that there appeared to be in the media a number of stories that might have been based on communications reporters or non-reporters like Rudy Giuliani were having with people in the New York field office," Comey later explained. "In particular, in, I want to say mid-October, maybe a little bit later, Mr. Giuliani was making statements that appeared to be based on his knowledge of workings inside the FBI New York. And then my recollection is there were other stories that were in the same ballpark that gave me a general concern that we may have a leak problem — unauthorized disclosure problem out of New York." The same day as Giuliani's Fox appearance, McCabe hastily arranged a conference call with Loretta Lynch, the attorney general, and the head of the FBI's New York office, who got "ripped by the AG on leaks," as he put it.

That same evening, Strzok and others briefed McCabe on the status of the Weiner laptop and their call with the New York case agent. Even though McCabe was going to be out of town the next day, they agreed it was urgent they brief Comey. Early the next morning — at 5:20 — McCabe emailed: "Boss, The [Midyear] team has come across some additional actions they believe they need to take. I think we should probably gather today to discuss implications if you have any space on your calendar. I am happy to join by phone. Will push to Lisa and Jim to coordinate if you are good."

Comey responded at 7:13 a.m., "Copy."

The Midyear team was waiting in Comey's conference room when the director walked in at 10 a.m. with a grin on his face. He had no idea what the meeting was about but was amused to see the familiar faces of the Midyear team gathered together once again. "The band is back together," he observed. The grin quickly faded as he saw the sober looks around the conference table.

McCabe had called in, but Baker suggested he hang up. Comey, too, told him to "drop off" the call and added, "I don't need you on this call." McCabe was taken aback and upset; he was the one who had pushed for the meeting, and was intimately involved in the issues, but he nonetheless hung up. Lisa Page also left the room. While Comey said nothing explicit, everyone realized their departures had something to do with the firestorm that had erupted after the Wall Street Journal article.

Strzok led the briefing, and briefly reviewed the bizarre sequence of events in which Anthony Weiner's sexually explicit texts to a 15-year-old minor led to the discovery of hundreds of thousands of Hillary Clinton emails. It all seemed new to Comey. None of it jogged any memory of his having been told about it before.

Comey asked what they'd found in the emails. "We see evidence of many, many, many, thousands and thousands of emails from the period of Secretary Clinton's tenure as Secretary of State," Comey recalled being told. "Two, we see Verizon.Blackberry.net email metadata. We don't know what the content is, from the period of time when Secretary Clinton was using a BlackBerry, Verizon.BlackBerry.net account at the beginning of her tenure as Secretary of State. We think this may be the missing three months of emails."

If so, it was a potent discovery, because Clinton's motive — the elusive question of her intent — would have been most evident when she began using the private server. Comey asked whether everyone felt they needed to review the contents of the emails to be sure they'd "turned over the necessary stones" and "be comfortable with the decision we made."

"Yes," Priestap replied. "We don't know with certainty what's in there. It could be information that we've not seen, you know, thus far, and so yes ... in effect it's dereliction of duty to not, you know this thing is out here to pass it over. So yes, we've got to, we have to do it."

"We don't know with certainty what's in there. It could be information that we've not seen, you know, thus far, and so yes ... in effect it's dereliction of duty to not, you know this thing is out here to pass it over. So yes, we've got to, we have to do it."

Strzok agreed, saying, "We have to pursue this material, because, you know, we would do it in any other case. And it is, you know, a pool of evidence that hypothetically, now understandably it's very speculative, but there is that possibility that it could change our outcome."

Priestap thought that unlikely. He felt that they'd already scoured so many emails that it was unlikely they'd discover any "smoking gun," as he put it, and if they did, it "would have shocked me." But "could it have been possible? Absolutely. That's why we had to review it."

Comey readily agreed that the FBI needed to get a search warrant and review the emails. As he later said, "We may be finding the golden missing emails that would change this case."

"How fast can you review and assess this?" Comey asked. The answer was weeks — maybe months — and not until long after the election. "Do it as quickly as you can, but do it well," Comey responded.

Going to court and applying for a warrant were overt investigative acts. Others would be aware or involved, including agents in New York and prosecutors in Preet Bharara's office. There was the ever-present danger, even likelihood, of leaks. According to Page, there was "a substantial and legitimate fear that when we went to seek the warrant in order to get access to the Weiner laptop, that the fact of that would leak."

More worrisome, with the decision to seek a warrant, Midyear "awoke from the dead," as Comey later put it. It was again an active investigation. The presidential election was exactly twelve days away. Comey wondered, now what?

The FBI obtained a search warrant for Weiner's laptop on October 30, and the computer was physically transferred from New York to the bureau's operational technology division in Quantico, Virginia. The laptop contained 1,355,980 items and approximately 650,000 emails. While that seemed a daunting and time-consuming task, technicians were able to narrow the Clinton-related emails to under 50,000. The FBI reviewed 6,827 emails that were either to or from Clinton and deemed 3,077 of those emails "potentially work-related." Strzok led the team that pored over each of these, working near 24-hour days. They found 13 email chains containing confidential information, though none were marked as classified. All were duplicates of emails that had already been examined.

On Friday, they told Comey they might finish before the election and made a final push to finish their review the next day. There was little disagreement that Comey should send another letter to Congress addressing the findings, although some worried that it was too close to the election to say anything. Strzok, for one, worried that anytime the FBI made an announcement, it only "reinvigorated" the news cycle, thrusting the FBI into the partisan wrangling. But there was no opposition at the Justice Department, which wanted Comey to correct any misimpressions that Clinton might still be charged, and lawyers there reviewed and signed off on a draft of the proposed letter.

The letter reached Congress on Sunday afternoon, November 6:

"I write to supplement my October 28, 2016 letter that notified you the FBI would be taking additional investigative steps with respect to former Secretary of State Clinton's use of a personal email server. Since my letter, the FBI investigative team has been working around the clock to process and review a large volume of emails from a device obtained in connection with an unrelated criminal investigation. During that process, we reviewed all of the communications that were to or from Hillary Clinton while she was Secretary of State. Based on our review, we have not changed our conclusions that we expressed in July with respect to Secretary Clinton. I am very grateful to the professionals at the FBI for doing an extraordinary amount of high-quality work in a short period of time."

As The Times put it, Comey's letter — his third public statement on the Midyear investigation — "swept away her largest and most immediate problem" but came "at the end of a rocky week for Mrs. Clinton that included wild, false speculation about looming indictments and shocking discoveries in the emails."

Trump immediately reverted to form — that Clinton "is being protected by a rigged system. It's a totally rigged system," as he said in Michigan on November 6 and at every subsequent rally.

Comey wanted nothing more to do with the election. He was, in his words, "too tired to care." He'd dedicated his career to the Department of Justice and the FBI in large part because they were institutions that stood apart from and above partisan politics. He had no plans to vote.

Comey had nonetheless achieved the dubious status of celebrity, or perhaps notoriety. That night he, his wife, and one of their daughters went out for dinner, where "Comey was spotted with a giant margarita at El Tio Tex Mex Grill," The Washington Post duly noted.

Perhaps because it drained the suspense from the Clinton email story rather than added to it, and had none of the "wild speculation" that had provided such good tabloid fare, Comey's November 6 letter got far less media attention. It wasn't even the lead news story that day; it was overshadowed by reports that a swarm of Secret Service agents had rushed Trump at a Nevada rally after someone in the crowd yelled, "Gun." (The man turned out to be unarmed.) The next day's news was dominated by the latest polls (which showed Clinton in a slight uptick, with a lead of 3.5 percentage points over Trump).

Strzok and Page never discussed any of their own work in terms of how it might affect the election. Strzok and Page had often advocated a tougher investigative approach toward Clinton and had even questioned issuing the November 6 letter exonerating her. None of their colleagues detected any hint of the political sentiments they expressed in what they assumed were confidential text messages.

Despite her lead in the polls, Page and Strzok weren't at all sure Clinton would win.

"The nyt probability numbers are dropping every day," a worried Page texted Strzok on November 3, referring to the Times's online forecast. "I'm scared for our organization."

"Stein and moron are F'ing everything up, too," Strzok replied, referring to the Green candidate, Jill Stein, and the Libertarian Gary Johnson. Four days later, The Times gave Clinton an 85% chance of winning, but added, "A victory by Mr. Trump remains possible." "OMG this is F*CKING TERRIFYING," Strzok texted.

For the first time since he was old enough, McCabe decided not to cast a vote.

Adapted from "Deep State: Trump, the FBI, and the Rule of Law" by James B. Stewart, published by Penguin Press.