#include <bits/stdc++.h>
using namespace std;
const bool print = false;
const int N = 57;
const long long mod = 998244353;
long long factorial[1100000];
long long invFactorial[1100000];
long long pow3MOD[1100000];
long long expMOD(long long a, long long exponent) {
  if (exponent == 0) {
    return 1;
  }
  if (exponent & 1) {
    return (a * expMOD(a, exponent - 1)) % mod;
  }
  long long halfPow = expMOD(a, exponent / 2);
  return (halfPow * halfPow) % mod;
}
long long nCr(long long n, long long r) {
  if (r < 0 || n < 0 || n < r) {
    return 0;
  }
  long long temp = factorial[n];
  temp = (temp * invFactorial[r]) % mod;
  temp = (temp * invFactorial[n - r]) % mod;
  return temp;
}
void initialiseArrays() {
  factorial[0] = 1;
  invFactorial[0] = 1;
  pow3MOD[0] = 1;
  for (long long i = 1; i < 1100000; i++) {
    factorial[i] = (i * factorial[i - 1]) % mod;
    invFactorial[i] = expMOD(factorial[i], mod - 2);
    pow3MOD[i] = (3 * pow3MOD[i - 1]) % mod;
  }
}
int main() {
  long long n;
  cin >> n;
  initialiseArrays();
  long long ans = 0;
  for (long long r = 1; r <= n; r++) {
    long long ncr = nCr(n, r);
    if (r & 1) {
      ncr = -ncr;
    }
    long long dp = ((pow3MOD[r] - 3) * expMOD(3, n * (n - r))) % mod;
    dp += (3 * expMOD(pow3MOD[n - r] - 1, n)) % mod;
    ans = (ans + (ncr * dp) % mod) % mod;
  }
  ans = (ans + expMOD(pow3MOD[n] - 3, n));
  ans = (expMOD(3, n * n) - ans) % mod;
  if (ans < 0) {
    ans += mod;
  }
  cout << ans << "\n";
}
