#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const ll MOD = 998244353;
vector<ll> combi;
ll modpow(ll x, ll p) {
  if (p == 0) return 1LL;
  if (p % 2) return (x * modpow(x, p - 1)) % MOD;
  ll half = modpow(x, p / 2);
  return (half * half) % MOD;
}
ll xgcd2(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll xt, yt, ret = xgcd2(b, a % b, xt, yt);
  x = yt;
  y = xt - (a / b) * yt;
  return ret;
}
ll getmodinv(ll x, ll p) { return modpow(x, p - 2); }
ll getmodinv2(ll x, ll p) {
  ll a, b;
  xgcd2(x, p, a, b);
  return a;
}
void initCombi(int n) {
  combi.resize(n + 1, 0);
  combi[0] = 1;
  for (int i = 1; i <= n; i++) {
    ll next = (combi[i - 1] * (n - i + 1)) % MOD;
    combi[i] = (getmodinv2(i, MOD) * next) % MOD;
    combi[i] = (combi[i] + MOD) % MOD;
  }
}
ll solve(ll n) {
  ll sol = 0;
  ll pow3 = 3;
  for (int j = 1; j <= n; j++) {
    ll temp = (j % 2 ? 1 : -1) * ((combi[j] * pow3) % MOD);
    temp = (temp * modpow(3, ((n - j) * n) % (MOD - 1))) % MOD;
    sol += temp;
    pow3 = (pow3 * 3) % MOD;
  }
  sol = (sol * 2) % MOD;
  ll sum = 0;
  ll mti = -modpow(3, 0);
  for (int i = 0; i <= n - 1; i++) {
    ll a = combi[i] * (i % 2 ? 1 : -1);
    ll b = (modpow(mti + 1, n) - modpow(mti, n)) % MOD;
    sum += (a * b) % MOD;
    mti = (mti * 3) % MOD;
  }
  sum = (sum * 3LL) % MOD;
  return (sol + sum + 2 * MOD) % MOD;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  initCombi(n);
  cout << solve(n) << '\n';
  return 0;
}
